本文整理汇总了Python中keras.utils.np_utils.to_categorical方法的典型用法代码示例。如果您正苦于以下问题:Python np_utils.to_categorical方法的具体用法?Python np_utils.to_categorical怎么用?Python np_utils.to_categorical使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.utils.np_utils
的用法示例。
在下文中一共展示了np_utils.to_categorical方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def setUp(self):
iris = load_iris()
theano.config.floatX = 'float32'
X = iris.data.astype(theano.config.floatX)
y = iris.target.astype(np.int32)
y_ohe = np_utils.to_categorical(y)
model = Sequential()
model.add(Dense(input_dim=X.shape[1], output_dim=5, activation='tanh'))
model.add(Dense(input_dim=5, output_dim=y_ohe.shape[1], activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='sgd')
model.fit(X, y_ohe, nb_epoch=10, batch_size=1, verbose=3, validation_data=None)
params = {'copyright': 'Václav Čadek', 'model_name': 'Iris Model'}
self.model = model
self.pmml = keras2pmml(self.model, **params)
self.num_inputs = self.model.input_shape[1]
self.num_outputs = self.model.output_shape[1]
self.num_connection_layers = len(self.model.layers)
self.features = ['x{}'.format(i) for i in range(self.num_inputs)]
self.class_values = ['y{}'.format(i) for i in range(self.num_outputs)]
示例2: nn_model
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def nn_model():
(x_train, y_train), _ = mnist.load_data()
# 归一化
x_train = x_train.reshape(x_train.shape[0], -1) / 255.
# one-hot
y_train = np_utils.to_categorical(y=y_train, num_classes=10)
# constant(value=1.)自定义常数,constant(value=1.)===one()
# 创建模型:输入784个神经元,输出10个神经元
model = Sequential([
Dense(units=200, input_dim=784, bias_initializer=constant(value=1.), activation=tanh),
Dense(units=100, bias_initializer=one(), activation=tanh),
Dense(units=10, bias_initializer=one(), activation=softmax),
])
opt = SGD(lr=0.2, clipnorm=1.) # 优化器
model.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['acc', 'mae']) # 编译
model.fit(x_train, y_train, batch_size=64, epochs=20, callbacks=[RemoteMonitor()])
model_save(model, './model.h5')
示例3: load_and_preprocess_data_3
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def load_and_preprocess_data_3():
# The data, shuffled and split between train and test sets:
(X_train, y_train), (x_test, y_test) = cifar10.load_data()
logging.debug('X_train shape: {}'.format(X_train.shape))
logging.debug('train samples: {}'.format(X_train.shape[0]))
logging.debug('test samples: {}'.format(x_test.shape[0]))
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
X_train = X_train.astype('float32')
x_test = x_test.astype('float32')
X_train /= 255
x_test /= 255
input_shape = X_train[0].shape
logging.debug('input_shape {}'.format(input_shape))
input_shape = X_train.shape[1:]
logging.debug('input_shape {}'.format(input_shape))
return X_train, x_test, y_train, y_test, input_shape
示例4: test_vector_clf
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def test_vector_clf(self):
nb_hidden = 10
print('vector classification data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(10,),
classification=True, nb_class=2)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Dense(X_train.shape[-1], nb_hidden))
model.add(Activation('relu'))
model.add(Dense(nb_hidden, y_train.shape[-1]))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), show_accuracy=True, verbose=2)
print(history.history)
self.assertTrue(history.history['val_acc'][-1] > 0.9)
示例5: test_temporal_clf
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def test_temporal_clf(self):
print('temporal classification data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(5,10),
classification=True, nb_class=2)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(GRU(X_train.shape[-1], y_train.shape[-1]))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), show_accuracy=True, verbose=2)
self.assertTrue(history.history['val_acc'][-1] > 0.9)
示例6: test_img_clf
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def test_img_clf(self):
print('image classification data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(3, 32, 32),
classification=True, nb_class=2)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Convolution2D(32, 3, 32, 32))
model.add(Activation('sigmoid'))
model.add(Flatten())
model.add(Dense(32, y_test.shape[-1]))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), show_accuracy=True, verbose=2)
self.assertTrue(history.history['val_acc'][-1] > 0.9)
示例7: get_cifar10
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def get_cifar10():
"""Retrieve the CIFAR dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 64
input_shape = (3072,)
# Get the data.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.reshape(50000, 3072)
x_test = x_test.reshape(10000, 3072)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例8: get_mnist
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def get_mnist():
"""Retrieve the MNIST dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 128
input_shape = (784,)
# Get the data.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
示例9: read_data
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def read_data(data_limit):
print "Reading Data..."
img_data = h5py.File(data_img)
ques_data = h5py.File(data_prepo)
img_data = np.array(img_data['images_train'])
img_pos_train = ques_data['img_pos_train'][:data_limit]
train_img_data = np.array([img_data[_-1,:] for _ in img_pos_train])
# Normalizing images
tem = np.sqrt(np.sum(np.multiply(train_img_data, train_img_data), axis=1))
train_img_data = np.divide(train_img_data, np.transpose(np.tile(tem,(4096,1))))
#shifting padding to left side
ques_train = np.array(ques_data['ques_train'])[:data_limit, :]
ques_length_train = np.array(ques_data['ques_length_train'])[:data_limit]
ques_train = right_align(ques_train, ques_length_train)
train_X = [train_img_data, ques_train]
# NOTE should've consturcted one-hots using exhausitve list of answers, cause some answers may not be in dataset
# To temporarily rectify this, all those answer indices is set to 1 in validation set
train_y = to_categorical(ques_data['answers'])[:data_limit, :]
return train_X, train_y
示例10: loadData
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def loadData(x_load_path, y_load_path):
# load train data
x_data_mat = sio.loadmat(x_load_path)
x_data_complex = x_data_mat['train_data']
x_data_real = x_data_complex.real
x_data_imag = x_data_complex.imag
x_data_real = x_data_real.reshape((x_data_real.shape[0], seqLen))
x_data_imag = x_data_imag.reshape((x_data_imag.shape[0], seqLen))
x_train = np.stack((x_data_real, x_data_imag), axis=2)
y_data_mat = sio.loadmat(y_load_path)
y_data = y_data_mat['train_label']
y_train = np_utils.to_categorical(y_data, nClass)
# train data shuffle
index = np.arange(y_train.shape[0])
np.random.shuffle(index)
x_train = x_train[index,:]
y_train = y_train[index]
return [x_train, y_train]
# fix random seed
示例11: get_data_generator
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def get_data_generator(data_iterator,
num_classes):
def get_arrays(db):
data = db.data[0].asnumpy()
if K.image_data_format() == "channels_last":
data = data.transpose((0, 2, 3, 1))
labels = to_categorical(
y=db.label[0].asnumpy(),
num_classes=num_classes)
return data, labels
while True:
try:
db = data_iterator.next()
except StopIteration:
# logging.warning("get_data exception due to end of data - resetting iterator")
data_iterator.reset()
db = data_iterator.next()
finally:
yield get_arrays(db)
示例12: cnn_example
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def cnn_example():
to_flatten = False
x_train, x_test, y_train, y_test, num_labels = extract_data(
flatten=to_flatten)
y_train = np_utils.to_categorical(y_train)
y_test_train = np_utils.to_categorical(y_test)
in_shape = x_train[0].shape
x_train = x_train.reshape(x_train.shape[0], in_shape[0], in_shape[1], 1)
x_test = x_test.reshape(x_test.shape[0], in_shape[0], in_shape[1], 1)
model = CNN(input_shape=x_train[0].shape,
num_classes=num_labels)
model.train(x_train, y_train, x_test, y_test_train)
model.evaluate(x_test, y_test)
filename = '../dataset/Sad/09b03Ta.wav'
print('prediction', model.predict_one(
get_feature_vector_from_mfcc(filename, flatten=to_flatten)),
'Actual 3')
print('CNN Done')
示例13: test_train
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def test_train(self):
train = pd.read_csv("/input/tests/data/train.csv")
x_train = train.iloc[:,1:].values.astype('float32')
y_train = to_categorical(train.iloc[:,0].astype('int32'))
model = Sequential()
model.add(Dense(units=10, input_dim=784, activation='softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer=RMSprop(lr=0.001),
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=1, batch_size=32)
# Uses convnet which depends on libcudnn when running on GPU
示例14: test_lstm
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def test_lstm(self):
x_train = np.random.random((100, 100, 100))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(100, 100)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(x_train, y_train, batch_size=32, epochs=1)
model.evaluate(x_test, y_test, batch_size=32)
示例15: conv_seq_labels
# 需要导入模块: from keras.utils import np_utils [as 别名]
# 或者: from keras.utils.np_utils import to_categorical [as 别名]
def conv_seq_labels(xds, xhs, nflips, model, debug, oov0, glove_idx2idx, vocab_size, nb_unknown_words, idx2word):
"""Convert description and hedlines to padded input vectors; headlines are one-hot to label."""
batch_size = len(xhs)
assert len(xds) == batch_size
x = [
vocab_fold(lpadd(xd) + xh, oov0, glove_idx2idx, vocab_size, nb_unknown_words)
for xd, xh in zip(xds, xhs)] # the input does not have 2nd eos
x = sequence.pad_sequences(x, maxlen=maxlen, value=empty, padding='post', truncating='post')
x = flip_headline(x, nflips=nflips, model=model, debug=debug, oov0=oov0, idx2word=idx2word)
y = np.zeros((batch_size, maxlenh, vocab_size))
for i, xh in enumerate(xhs):
xh = vocab_fold(xh, oov0, glove_idx2idx, vocab_size, nb_unknown_words) + [eos] + [empty] * maxlenh # output does have a eos at end
xh = xh[:maxlenh]
y[i, :, :] = np_utils.to_categorical(xh, vocab_size)
return x, y