def compile(name, model: Sequential, train_samples: pd.DataFrame, validation_samples: pd.DataFrame, gen, type='img'): # model.add(Reshape((-1, num_classes), name=RESHAPED)) size = 5 steps_per_epoch = len(train_samples) // size validation_steps = len(validation_samples) // size train_generator = gen(train_samples, type)(size, infinite=True) validation_generator = gen(validation_samples, type)(size, infinite=True) adam = optimizers.Adam(lr=0.0001) model.compile(loss='categorical_crossentropy', optimizer=adam) history_object = model.fit_generator(train_generator, validation_data=validation_generator, epochs=5, callbacks=None, validation_steps=validation_steps, steps_per_epoch=steps_per_epoch) model.save_weights(name) # model.save('fcn_model.h5') print(history_object.history.keys()) print('Loss') print(history_object.history['loss']) print('Validation Loss') print(history_object.history['val_loss'])
def get_model(idrop=0.2, edrop=0.1, odrop=0.25, rdrop=0.2, weight_decay=WEIGHT_DECAY): model = Sequential() model.add( Embedding( NB_WORDS, 128, embeddings_regularizer=l2(weight_decay), input_length=MAXLEN)) # , batch_input_shape=(batch_size, maxlen))) if edrop: model.add(Dropout(edrop)) model.add( LSTM(128, kernel_regularizer=l2(weight_decay), recurrent_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay), dropout=idrop, recurrent_dropout=rdrop)) if odrop: model.add(Dropout(odrop)) model.add( Dense(1, kernel_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay), activation='sigmoid')) optimizer = Adam(1e-3) model.compile(loss='binary_crossentropy', metrics=["binary_accuracy"], optimizer=optimizer) return model
def get_model(idrop=0.2, edrop=0.1, odrop=0.25, rdrop=0.2, weight_decay=1e-4, lr=1e-3): model = Sequential() model.add( Embedding(NB_WORDS, 128, embeddings_regularizer=l2(weight_decay), input_length=MAXLEN)) if edrop: model.add(Dropout(edrop)) model.add( LSTM(128, kernel_regularizer=l2(weight_decay), recurrent_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay), dropout=idrop, recurrent_dropout=rdrop)) if odrop: model.add(Dropout(odrop)) model.add( Dense(1, kernel_regularizer=l2(weight_decay), bias_regularizer=l2(weight_decay))) optimizer = Adam(lr) model.compile(loss='mse', metrics=["mse"], optimizer=optimizer) return model
def _build_model(self): model = Sequential() model.add(Dense(3, input_dim=2, activation='tanh')) model.add(Dense(3, activation='tanh')) model.add(Dense(self.env.action_space.n, activation='linear')) model.compile(loss='mse', optimizer=Adam(lr=self.alpha, decay=self.alpha_decay)) return model
def main(): x_train, y_train, x_test, y_test = load_data() model = Sequential() model.add( Conv2D(32, kernel_size=(11, 11), strides=4, padding="same", activation='relu', input_shape=(48, 48, 1))) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding="valid")) model.add( Conv2D(32, kernel_size=(5, 5), strides=1, padding="same", activation='relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=2, padding="valid")) model.add( Conv2D(32, kernel_size=(3, 3), strides=1, padding="same", activation='relu')) model.add( Conv2D(32, kernel_size=(3, 3), strides=1, padding="same", activation='relu')) model.add( Conv2D(32, kernel_size=(3, 3), strides=1, padding="same", activation='relu')) model.add(Dense(1024, activation='relu')) model.add(Dense(512, activation='relu')) model.add(Dense(7, activation='softmax')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=128, epochs=5, verbose=1, validation_data=(x_test, y_test)) model.save(expanduser("~/emotion/alex_net.h5")) accuracy, fbeta = test_model(model, x_test, y_test) print("Accuracy: %s" % accuracy) print("F-Beta: %s" % fbeta)
def _build_model(self): # Neural Net for Deep-Q learning Model model = Sequential() model.add(Dense(24, input_dim=self.state_size, activation='relu')) model.add(Dense(24, activation='relu')) model.add(Dense(self.action_size, activation='linear')) model.compile(loss=self._huber_loss, optimizer=Adam(lr=self.learning_rate)) return model
def fit_lstm(train, batch_size, nb_epoch, neurons): X, y = train[:, 0:-1], train[:, -1] X = X.reshape(X.shape[0], 1, X.shape[1]) model = Sequential() model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') for i in range(nb_epoch): model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False) model.reset_states() return model
class WindPuller(object): def __init__(self, input_shape, lr=0.01, n_layers=2, n_hidden=8, rate_dropout=0.2, loss=risk_estimation): print("initializing..., learing rate %s, n_layers %s, n_hidden %s, dropout rate %s." % ( lr, n_layers, n_hidden, rate_dropout)) self.model = Sequential() self.model.add(Dropout(rate=rate_dropout, input_shape=(input_shape[0], input_shape[1]))) for i in range(0, n_layers - 1): self.model.add(LSTM(n_hidden * 4, return_sequences=True, activation='tanh', recurrent_activation='hard_sigmoid', kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', dropout=rate_dropout, recurrent_dropout=rate_dropout)) self.model.add(LSTM(n_hidden, return_sequences=False, activation='tanh', recurrent_activation='hard_sigmoid', kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros', dropout=rate_dropout, recurrent_dropout=rate_dropout)) self.model.add(Dense(1, kernel_initializer=initializers.glorot_uniform())) # self.model.add(BatchNormalization(axis=-1, moving_mean_initializer=Constant(value=0.5), # moving_variance_initializer=Constant(value=0.25))) self.model.add(BatchNormalization(axis=-1)) self.model.add(Activation("relu(alpha=0., max_value=1.0)")) opt = RMSprop(lr=lr) self.model.compile(loss=loss, optimizer=opt, metrics=['accuracy']) def fit(self, x, y, batch_size=32, nb_epoch=100, verbose=1, callbacks=None, validation_split=0., validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0): self.model.fit(x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch) def save(self, path): self.model.save(path) def load_model(self, path): self.model = load_model(path) return self def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None, **kwargs): return self.model.evaluate(x, y, batch_size, verbose, sample_weight) def predict(self, x, batch_size=32, verbose=0): return self.model.predict(x, batch_size, verbose)
def build_network(row, col, channels, num_class): model = Sequential() model.add( Conv2D(32, (8, 8), (4, 4), activation='relu', input_shape=(row, col, channels))) model.add(Conv2D(64, (4, 4), (2, 2), activation='relu')) model.add(Conv2D(64, (3, 3), (1, 1), activation='relu')) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dense(num_class)) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) return model
def main(): start = time.time() # generate multiple time-series sequences dataframe = generate_sine_data() dataset = dataframe.values.astype('float32') # put dataset of multiple time-series sequences dataset = Normalize(dataset) # create dataset length = len(dataset) train_size = int(length * 0.67) test_size = length - train_size train, test = dataset[:train_size], dataset[train_size:] trainX, trainY = create_dataset(train) testX, testY = create_dataset(test) trainX = trainX[len(trainX) % BATCH_SIZE:] trainY = trainY[len(trainY) % BATCH_SIZE:] length_test = len(testX) testX = testX[len(testX) % BATCH_SIZE:] testY = testY[len(testY) % BATCH_SIZE:] trainX = np.reshape(trainX, (trainX.shape[0], trainX.shape[1], 3)) testX = np.reshape(testX, (testX.shape[0], testX.shape[1], 3)) # construct the DNN model (LSTM + fully_connected_layer) model = Sequential() model.add(LSTM(HIDDEN_SIZE, batch_input_shape=(BATCH_SIZE, Tau, 3))) model.add(Dense(3)) model.summary() model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy']) # learn the DNN model on training dataset hist = model.fit(trainX, trainY, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=0, shuffle=True) # plot the leargning curve if PLT: epochs = range(1, 11) plt.figure() plt.plot(epochs, hist.history['loss'], label='loss/training') plt.plot(epochs, hist.history['acc'], label='acc/training') plt.xlabel('epoch') plt.ylabel('acc / loss') plt.legend() plt.show() plt.close() # evaluate the DNN model on test dataset score = model.evaluate(testX, testY, batch_size=BATCH_SIZE, verbose=0) print('loss: {0[0]}, acc: {0[1]} on test dataset'.format(score)) # forecast Ls-steps-ahead value on the test dataset predicted = model.predict(testX, batch_size=BATCH_SIZE) # plot testY and predictedY if PLT: df_out = pd.DataFrame(predicted[:200]) df_out.columns = [ "predicted_sine", 'predicted_sine_rand', 'predicted_sine_int' ] df_out = pd.concat([ df_out, pd.DataFrame( testY[:200], columns=["input_sine", "input_sine_rand", "input_sine_int"]) ]) plt.figure() df_out.plot() plt.show() plt.close() # plot the forecasting results on test dataset if PLT: plt.ion() i = 0 while i < 20: K = 3 # the number of sensor fig = plt.figure(figsize=(8, K * 4)) plt.subplots_adjust(hspace=0.2) for j in range(K): plt.subplot(K, 1, j + 1) plt.plot(range(i, i + Tau + Ls + 1), test[length_test % BATCH_SIZE:][i:i + Tau + Ls + 1, j], color='silver', label='original') plt.plot(range(i, i + Tau), testX[i, :, j], color='dodgerblue', label='input') plt.scatter(i + Tau + Ls, predicted[i, j], s=15, color='orange', label='forecast') plt.legend() plt.draw() plt.pause(1.2) plt.clf() i += 1 plt.close() end = time.time() print('elapsed_time: {}[s]'.format(end - start))
# For a single-input model with 2 classes (binary classification): """ from tensorflow.contrib.keras.python.keras.models import Sequential from tensorflow.contrib.keras.python.keras.layers import Dense, Activation # Generate dummy data import numpy as np data = np.random.random((1000, 100)) labels1 = np.random.randint(2, size=(1000, 1)) model = Sequential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # Train the model, iterating on the data in batches of 32 samples print("\nNo validation_set split") model.fit(data, labels1, epochs=1, batch_size=32) ####################################### # For a single-input model with 10 classes (categorical classification): # Generate dummy data import numpy as np data = np.random.random((1000, 100)) labels2 = np.random.randint(10, size=(1000, 1)) from tensorflow.contrib.keras.python.keras.utils import to_categorical # Convert labels to categorical one-hot encoding one_hot_labels = to_categorical(labels2, num_classes=10)
seq.add(BatchNormalization()) seq.add( ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same', return_sequences=True)) seq.add(BatchNormalization()) seq.add( Conv3D(filters=1, kernel_size=(3, 3, 3), activation='sigmoid', padding='same', data_format='channels_last')) seq.compile(loss='binary_crossentropy', optimizer='adadelta') # Artificial data generation: # def generate_movies(n_samples=1200, n_frames=15): row = 80 col = 80 noisy_movies = np.zeros((n_samples, n_frames, row, col, 1), dtype=np.float) shifted_movies = np.zeros((n_samples, n_frames, row, col, 1), dtype=np.float) for i in range(n_samples): # Add 3 to 7 moving squares n = np.random.randint(3, 8)
from tensorflow.contrib.keras.python.keras.models import Sequential from tensorflow.contrib.keras.python.keras.layers import Dense, Dropout, Activation from tensorflow.contrib.keras.python.keras.optimizers import SGD from tensorflow.contrib.keras.python.keras.utils import to_categorical import numpy as np # Generate dummy data x_train = np.random.random((1000, 20)) y_train = np.random.randint(2, size=(1000, 1)) x_test = np.random.random((100, 20)) y_test = np.random.randint(2, size=(100, 1)) model = Sequential() model.add(Dense(64, input_dim=20, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', # binary classification optimizer='rmsprop', metrics=['accuracy']) hist = model.fit(x_train, y_train, validation_split=0.2, epochs=1, batch_size=128) hist.history score = model.evaluate(x_test, y_test, batch_size=128)
y_train = to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10) x_test = np.random.random((100, 20)) y_test = to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10) model = Sequential() # Dense(64) is a fully-connected layer with 64 hidden units. # in the first layer, you must specify the expected input data shape: # here, 20-dimensional vectors. model.add(Dense(64, activation='relu', input_dim=20)) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax')) # specify optimizer sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) # param adjust model.compile( loss='categorical_crossentropy', # multi-class optimizer=sgd, metrics=['accuracy']) hist = model.fit(x_train, y_train, validation_split=0.2, epochs=1, batch_size=128) print(hist.history) score = model.evaluate(x_test, y_test, batch_size=128)
#create train and test lists. X-patterns, Y-intents train_x = list(training[:, 0]) train_y = list(training[:, 1]) #print("Training data created") #create a model Tensorflow model = Sequential() model.add(Dense(128, input_shape=(len(train_x[0]), ), activation='relu')) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(len(train_y[0]), activation='softmax')) #compile model. Stochastic gradient descent with nesterov accelerated gradient gives good result sgd = SGD(lr=0.01, decay=1e-6, momentum=0.5, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd) #fitting and saving model hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1) model.save('chatbot_model.h5', hist) print("model created")
# create model if os.path.isfile(savefile): model = load_model(savefile) else: model = Sequential() model.add(Dense(128, input_dim=input_dimension, kernel_initializer=hidden_initializer, activation='relu')) #model.add(Dense(128, input_dim=input_dimension, activation='relu')) model.add(Dropout(dropout_rate)) model.add(Dense(64, kernel_initializer=hidden_initializer, activation='relu')) model.add(Dense(2, kernel_initializer=hidden_initializer, activation='softmax')) #model.add(Dense(64, activation='relu')) #model.add(Dense(2, activation='softmax')) sgd = SGD(lr=learning_rate, momentum=momentum) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['acc']) model.fit(X_train, y_train, epochs=50, batch_size=128) predictions = model.predict_proba(X_test) ans = pd.DataFrame(predictions,columns=['target','dmy']) print ans outdf = pd.concat([outdf,ans['target']],axis=1) outdf.to_csv('./submit_keras.csv',index=False) #save the model #model_json = model.to_json() #with open("./ans.json", "w") as json_file: # json_file.write(model_json)
model = Sequential() # if return_sequences = Flase, only last LSTM cell will slpit the output. model.add(LSTM(32, input_shape=(look_back, data_dim), return_sequences=True)) model.add(LSTM(2)) #model.add(Dense(10)) model.add(Dropout(0.2)) model.add(Dense(1, activation='softmax')) # try using different optimizers and different optimizer configs model.compile('adam', 'binary_crossentropy', metrics=['accuracy']) time_train_X, time_train_y = create_dataset(w2v_tfIdf_train_X, train_y, look_back=look_back) time_test_X, time_test_y = create_dataset(w2v_tf_Idf_test_X, test_y, look_back=look_back) model.fit(time_train_X, time_train_y, verbose=2, epochs=10, batch_size=100, validation_data=[time_test_X, time_test_y])
def make_vgg16(top=True, weights='imagenet', weight_path=''): """ Args: top determines whether to include dense layers at the top. Weights determines whether to use imagenet weights or pre-trained weights, in which case the filepath must be specified via weight_path. Creates a convolutional neural network following the VGG16 structure. There are two options: the original structure with fully connected layers at the end, or a slimmed down model where the FC layers are replaced by a global average pooling layer. The latter has far fewer weights. Returns the model. """ model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 3))) model.add(Convolution2D(64, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) if weights == 'imagenet': model.add(Flatten()) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1000, activation='softmax')) model.load_weights(r'D:/drivers/vgg16_weights.h5') if top == False: for i in range(7): model.layers.pop() model.outputs = [model.layers[-1].output] model.layers[-1].outbound_nodes = [] model.add(Convolution2D(1024, (3, 3), activation='relu')) model.add(AveragePooling2D((14, 14), padding='same')) model.add(Flatten()) model.add(Dense(10, activation='softmax')) else: model.layers.pop() model.outputs = [model.layers[-1].output] model.layers[-1].outbound_nodes = [] model.add(Dense(10, activation='softmax')) elif weights == 'trained': model.add(Flatten()) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax')) if top == False: for i in range(7): model.layers.pop() model.outputs = [model.layers[-1].output] model.layers[-1].outbound_nodes = [] model.add(Convolution2D(1024, (3, 3), activation='relu')) model.add(AveragePooling2D((14, 14), padding='same')) model.add(Flatten()) model.add(Dense(10, activation='softmax')) model.load_weights(weight_path) sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='sparse_categorical_crossentropy', metrics=['accuracy']) return model
max_features = 20000 maxlen = 100 batch_size = 32 (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=max_features) X_train = sequence.pad_sequences(X_train, maxlen=maxlen) X_test = sequence.pad_sequences(X_test, maxlen=maxlen) print('X_train shape:', X_train.shape) print('X_test shape:', X_test.shape) model = Sequential() model.add(Embedding(max_features, 128, input_length=maxlen)) #model.add(SimpleRNN(128)) #model.add(GRU(128)) model.add(LSTM(128)) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') model.fit(X_train, y_train, batch_size=batch_size, epochs=1, validation_data=(X_test, y_test))
uniques, ids = np.unique( arr, return_inverse=True) # convert 3 words into 0, 1, 2 return to_categorical(ids, len(uniques)) # convert 0, 1, 2 to one-hot train_y_ohe = one_hot_encode_object_array(train_y) test_y_ohe = one_hot_encode_object_array(test_y) model = Sequential() model.add(Dense(16, input_shape=(4, ))) # each sample has 4 features model.add(Activation('sigmoid')) # add non-linearity to hidden layer 1 model.add(Dense(3)) # add another 3 neuron final layer model.add(Activation('softmax')) # give it non-linearity as output model.summary() model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"]) model.fit(train_X, train_y_ohe, validation_split=0.2, epochs=10, batch_size=1, verbose=1) loss, accuracy = model.evaluate(test_X, test_y_ohe, batch_size=32, verbose=1) print("Accuracy = {:.2f}".format(accuracy))
def create_model(): model = Sequential() model.add(Dense(12, input_dim=8, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
w_1_2_2.append(init_weights[0][1, 1]) w_1_3_1.append(init_weights[0][0, 2]) w_1_3_2.append(init_weights[0][1, 2]) w_1_4_1.append(init_weights[0][0, 3]) w_1_4_2.append(init_weights[0][1, 3]) w_2_1.append(init_weights[2][0, 0]) w_2_2.append(init_weights[2][1, 0]) w_2_3.append(init_weights[2][2, 0]) w_2_4.append(init_weights[2][3, 0]) losses = [] accuracies = [] # use SGD optimizer with learning rate 0.1 sgd = SGD(lr=0.1) # set loss function to be mse, print out accuracy model.compile(loss='mse', optimizer=sgd, metrics=['accuracy']) # set loss function to be binary_crossentropy, print accuracy model1.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) ####################################################### # batch_size = 1, update weights every sample; # batch_size = 100, update weights every 100 sample; # without validation_split, all dataset are trained # with validation_split=0.25, 25% dataset is saved for validation,rest for training; during training, there will be loss and val_loss, accu, and val_accu # shuffle = True, all samples of training set will be shuffled for each epoch training # epochs = 10, train with the entire dataset for 10 times # hist = fit() will record a loss for each epoch ####################################################### # hist1 = model.fit(X, y, batch_size=1, validation_split=0.25, epochs=10) # accuracy 0.75 # hist2 = model.fit(X, y, batch_size=1, epochs=1000) # accuracy 100%, loss keep dropping down to 0.0016
input_shape=(20, 20, 1), activation="relu")) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # Second convolutional layer with max pooling model.add(Conv2D(50, (5, 5), padding="same", activation="relu")) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # Hidden layer with 500 nodes model.add(Flatten()) model.add(Dense(500, activation="relu")) # Output layer with 32 nodes (one for each possible letter/number we predict) model.add(Dense(32, activation="softmax")) # Ask Keras to build the TensorFlow model behind the scenes model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) # Train the neural network model.fit(X_train, Y_train, validation_data=(X_test, Y_test), batch_size=32, epochs=10, verbose=1) # Save the trained model to disk model.save(MODEL_FILENAME)
test = values[n_train_hours:, :] # split into input and outputs train_X = train[:, :-1] train_y = train[:, -1] test_X = test[:, :-1] test_y = test[:, -1] # reshape input to be 3D [samples, timesteps, features] train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1])) test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1])) print(train_X.shape, train_y.shape, test_X.shape, test_y.shape) # design network model = Sequential() model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]))) model.add(Dense(1)) model.compile(loss='mae', optimizer='adam') # fit network history = model.fit(train_X, train_y, epochs=50, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False) # plot history pyplot.plot(history.history['loss'], label='Training Loss') pyplot.plot(history.history['val_loss'], label='Validation Loss') pyplot.legend() pyplot.show()
model.add(Embedding(input_dim=64, output_dim=256, input_length=10)) # input_dim: Size of the vocabulary # input_length: Length of input sequences, length of each sentences # output_dim: Dimension of the dense embedding model.input # (?, 10), model.output # (?, 10, 256) model.add(LSTM(128)) # unit=128, dimensionality of the output space model.output model.add(Dropout(0.5)) # percent to drop out # model.ouput # will cause error on Dropout model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) import numpy as np x_train = np.random.random((1000, 10)) y_train = np.random.randint(2, size=(1000, 1)) x_test = np.random.random((100, 10)) y_test = np.random.randint(2, size=(100, 1)) hist=model.fit(x_train, y_train, validation_split=0.2, batch_size=16, epochs=1) hist.history score = model.evaluate(x_test, y_test, batch_size=16) """ Embedding
model.summary() model = Sequential() model.add(Dense(32, input_shape=(784, 10))) # return tensor (?, 784, 32) model.add(Dense(1)) # return tensor shape (?, 784, 1) model.summary() import numpy as np x = np.random.random((100, 784, 10)) y = np.random.random((100, 784, 1)) """ ## Compilation - optimzier: 'rmsprop', 'SGD', ... - loss: 'mse', 'categorical_crossentropy', 'binary_crossentropy', ... - metrics: 'accuracy', ... or user_made_func """ # For custom metrics from tensorflow.contrib.keras.python.keras import backend as K def mean_pred(y_true, y_pred): # score_array = fn(y_true, y_pred) must 2 args return K.mean(y_pred) # For a binary classification problem model.compile(optimizer='rmsprop', loss='mse', metrics=['accuracy', mean_pred]) hist = model.fit(x, y, validation_split=0.3, epochs=2) print(hist.history)
dp_array.shape # compare to see for equality (dp_array == dp_array_train).sum() # total differ ### both BatchNormalization and Droput have some basic operations prior to Normalization and Droping, Diving into the source when feeling so #### Access middle layer output when First Dropout and then BatchNormalization model_seq = Sequential() model_seq.add(Dropout(0.3, input_shape=(10, ))) model_seq.add(BatchNormalization()) model_seq.add(Dense(1)) # check out weights before training # model_seq.get_weights() # compile and train model_seq.compile(optimizer='SGD', loss='mse') model_seq.fit(input_array_small, target_small, epochs=10) model_seq.get_weights() model_seq.save("to_delete.h5") model_best = load_model("to_delete.h5") ###### compare two weights from two different training # model_seq.get_weights() ###### check output batchNorm_test = K.function( [model_best.input, K.learning_phase()], [model_best.layers[-2].output])([input_array_small, 0])[0] batchNorm_train = K.function( [model_best.input, K.learning_phase()],
print('x_train shape :', x_train.shape) print('x_test shape :', x_test.shape) print('y_train shape :', y_train.shape) print('y_test shape :', y_test.shape) batch_size = 32 epochs = 5 model = Sequential() model.add(Dense(512, input_shape=(max_words, ))) model.add(Activation('relu')) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_split=0.1) score = model.evaluate(x_test, y_test, batch_size=batch_size, verbose=1) print('Test score:', score[0]) print('Test accuracy:', score[1]) y_train
class MLP_keras: def __init__(self, learning_rate, layers, functions, optimizer_name, beta=0.0, dropout=1.0): self.n_input = layers[0] self.n_hidden = layers[1:-1] self.n_output = layers[-1] self.model = Sequential() if len(self.n_hidden) == 0: # single layer self.model.add(Dense(self.n_output, activation=functions[0], kernel_regularizer=regularizers.l2(beta), input_shape=(self.n_input,))) elif len(self.n_hidden) == 1: # hidden layer self.model.add(Dense(self.n_hidden[0], activation=functions[0], kernel_regularizer=regularizers.l2(beta), input_shape=(self.n_input,))) self.model.add(Dropout(dropout)) # output layer self.model.add(Dense(self.n_output, activation=functions[1], kernel_regularizer=regularizers.l2(beta))) else: # the first hidden layer self.model.add(Dense(self.n_hidden[0], activation=functions[0], kernel_regularizer=regularizers.l2(beta), input_shape=(self.n_input,))) self.model.add(Dropout(dropout)) # the second hidden layer self.model.add(Dense(self.n_hidden[1], activation=functions[1], kernel_regularizer=regularizers.l2(beta))) self.model.add(Dropout(dropout)) # the output layer self.model.add(Dense(self.n_output, activation=functions[2], kernel_regularizer=regularizers.l2(beta))) self.model.summary() if optimizer_name == 'Adam': optimizer = Adam(learning_rate) #self.model.compile(loss='mean_squared_error', # optimizer=optimizer, # metrics=['accuracy']) self.model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) def train(self, epochs, trn, vld=None, batch_size=32, es=0): if vld is not None: validation_data = (vld.x, vld.y) if es > 0: callbacks = [EarlyStopping(monitor='val_loss', patience=es)] else: callbacks = None else: validation_data = None callbacks = None self.model.fit(trn.x, trn.y, batch_size=batch_size, epochs=epochs, verbose=2, callbacks=callbacks, validation_data=validation_data) loss, tst_acc = self.model.evaluate(trn.x, trn.y, verbose=0) if vld is not None: _, vld_acc = self.model.evaluate(vld.x, vld.y, verbose=0) else: vld_acc = 0 return ['', loss, tst_acc, vld_acc, 0] def evaluate(self, x_test, y_test): score = self.model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) def score(self, tst): return self.model.evaluate(tst.x, tst.y, verbose=0)[1] def predict_proba(self, x): if x is None: return None return self.model.predict_proba(x, verbose=0)