Esempio n. 1
0
	def _construct_network(self):
		model = Sequential()
		model.add(Convolution2D(32,3,3)
		model.add(Activation('relu'))
		model.add(MaxPooling2D(pool_size=(2,2)))
		model.add(Activation('relu'))
		model.add(Dropout(0.4))
		model.add(Convolution2D(64,3,3, border_mode='full'))
		model.add(Activation('relu'))
		model.add(MaxPooling2D(pool_size=(2,2)))
		model.add(Dropout(0.2))
		model.add(Flatten())
		model.add(Dense(512))
		model.add(Dropout(0.5))
		model.add(Dense(self.num_classes))
		model.add(Activation('softmax'))

	def train(self, X, y, Xtest, ytest):
		model = self._construct_network()
		model.compile(loss='categorical_crossentropy', optimizer='adam')
		X = X.astype("float32")
		Xtest = X.astype("float32")
		X /=255
		Xtest /= 255
		model.fit(X, y)
from keras.optimizers import SGD, RMSprop, Adam
from keras import metrics
np.random.seed(0)

train_x = np.linspace(0, np.pi * 2, 20).reshape(20, 1)
train_y = np.sin(train_x)

test_x = np.linspace(0, np.pi * 2, 20).reshape(60, 1)
test_y = np.sin(test_x)

input_node = 1
hidden1_node = 3
output_node = 1

model = Sequential()
model.add(Dense(hidden1_node, input_dim=input_node, activation='sigmoid'))
model.add(Dense(output_node))

optimzier_option = {
    'sgd': SGD(lr=0.1),
    'momentum': SGD(lr=alpha, momentum=0.9),
    'RMSProp': RMSprop(lr=0.01),
    'Adam': Adam(lr=0.1)
}

result = []
train_y_hat = []

test_y_hat = []
for optimizer_name, optimizer_setting in optimizer_option.items():
Esempio n. 3
0
	def train(self, X, y, Xtest, ytest, modelname='GRU', batch_size=32):
		model = Sequential()
		if modelname == 'GRU':
			model.add(GRU(output_dim=self.hid_dim, return_sequence=True, input_shape=(maxvalue, self.word_dim)))
		if modelname == 'LSTM':
			model.add(LSTM(output_dim=self.hid_dim, return_sequence=True, input_shape=(maxvalue, self.word_dim)))
		else:
			raise Exception("Model name is not found")
		model.add(Dropout(0.5))
		model.add(Activation('sigmoid'))
		model.compile(loss='categorical_crossentropy', optimizer='adadelta')
		model.fit(X,y, batch_size=batch_size, nb_epoch=4, validation_data=(Xtest, ytest), show_accuracy=True)
		score, acc = model.evaluate(Xtest, ytest, batch_size=batch_size, show_accuracy=True)
		print('Score: {0}'.format(score))
		print('Accuracy: {0}'.format(acc))