def build_model(layers): model = Sequential() model.add( LSTM(input_shape=(layers[1], layers[0]), output_dim=layers[1], return_sequences=True)) model.add(Dropout(0.2)) model.add(LSTM(layers[2], return_sequences=False)) model.add(Dropout(0.2)) model.add(Dense(output_dim=layers[3])) model.add(Activation("linear")) start = time.time() model.compile(loss="mse", optimizer="rmsprop") print("> Compilation Time : ", time.time() - start) return model
classifier.add( Dense( units=128, kernel_initializer = 'uniform', activation = 'relu', input_dim = X_train.shape[1])) # first layer need to assign 'input_dim' classifier.add( Dense( units=128, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=128, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=64, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=64, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=64, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=32, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=32, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=32, kernel_initializer = 'uniform', activation = 'relu')) classifier.add( Dense( units=1, kernel_initializer = 'uniform', activation = 'linear')) ################### part eleven -- define optimizer ############################### RMSprop = optimizers.RMSprop(lr=0.001) # can tune learning rate here ################## part twelve -- compile model ################################# classifier.compile(optimizer = RMSprop, loss = 'mean_squared_error', metrics = ['accuracy']) # can add more metrics, can change loss based on purpose ################# part thirteen -- set up early stoppping and prepare for history recording ################################## from keras.callbacks import EarlyStopping, TensorBoard early_stopping = EarlyStopping(monitor='val_loss', patience = 100) tensorBoard = TensorBoard(log_dir={path to store log file}, histogram_freq=0, batch_size=128) ''' Do we need to keep the batch_size of tensorBoard and the training classifier part SAME? ''' #################### part fourteen -- fitting the model to training data set ################################################ hist = classifier.fit(X_train, y_train, batch_size=512, epochs=5000, validation_split=0.05) # keep track of ending time end = datetime.now()
Output = Test.iloc[:, 2].values # Setting up dataframes for 10 - cross validation from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(NN_Data, Output, test_size = 0.1,random_state = 0) # feature scaling from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) # NEURAL NETWORK TIME! import keras from keras.model import Sequential from keras.layers import Dense NN = Sequential() # Input and 1st hidden layer NN.add(Dense(output_dim = 2, init = 'uniform', activation = 'relu', input_dim = 3)) # Second hidden layer NN.add(Dense(output_dim = 1, init = 'uniform', activation = 'relu')) # training the NN NN.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy']) NN.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)
x = trainDataset[:-1, :] y = trainDataset[1:, :] from keras.model import Sequential from keras.layers import Dense from keras.layers import LSTM model = Sequential() model.add(LSTM(units=32, activation='sigmoid', input_shape=(None, 1))) model.add(Dense(units=1)) model.compile(optimizer='adam', loss='mean_squared_error') model.fit(x, y, epochs=100, batch_size=32) realStock = pd.read_csv('Google_Stock_Price_Test.csv') realStock = realStock.iloc[:, 4:5].values plt.plot(realStock, 'r')
from keras.model import Sequential from keras.layers import Dense, Activation model = Sequential([Dense(32, input_shape=(784,)), Activation('relu'), Dense(10), Activation('softmax'),]) # multi classes model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # binary classes model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # mean square root model.compile(optimizer='rmsprop', loss='mse') # user defined metrics def mean_pred(y_true, y_pred) return K.mean(y_pred) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy', mean_pred]) # train # generate dataset # binary import numpy as np data = np.random.random((1000, 100)) labels = np.random.randint(2, size=(1000, 1)) model = Squential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop', loss = 'binary_crossentropy', metrics=['accuracy'])
#we cant directly apply the 2D image to te NN -> so have to flatten the image-> 1x (28x28) #convert 2D image to flatten layer #784 neurons for the first layer #this is the first inpiut layer model.add(Flatten(input_shape=(28,28))) #this is a classification problem model.add(Dense(512,activate='relu')) model.add(Dense(256,activate='relu')) model.add(Dense(128,activate='relu')) model.add(Dense(64,activate='relu')) model.add(Dense(10,activate='softmax')) #here we have 10 types of numbers: 0-9 #compile the model model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) model.summary() # In[ ]: #categorical conversion from keras.utils import np_utils #train_target,test_target will be converted to categoical form new_train_target = np_utils.to_categorical(train_target) new_test_target = np_utils.to_categorical(test_target) print(train_target[:10])
from keras.model import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(100, input_dim=128, init='uniform', activation='relu')) model.add(Dense(100, init='uniform', activation='relu')) model.add(Dense(1, init='uniform', activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, Y_train, validation_data=(X_test, Y_test), nb_epoch=500, batch_size=10, verbose=2) #training accuracy scores = model.evaluate(X_train, Y_train) print("Accuracy: %.2f%%" % (scores[1] * 100)) #testing accuracy scores2 = model.evaluate(X_test, Y_test) print("Accuracy: %.2f%%" % (scores2[1] * 100))
all_train_x = all_train_x.astype('float32') all_test_x = all_test_x.astype('float32') all_train_y = np_utils.to_categorical(all_train_y, classes_count) all_test_y = np_utils.to_categorical(all_test_y, classes_count) model = Sequential() model.add(Convolution2D(32,3,3,border_mode = 'same', input_shape=all_train_x.shape[1:])) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size = (2,2)) model.add(Dropout(0,25)) model.add(Convolution2D(64,3,3,border_mode = 'same')) model.add(Activation('relu')) model.add(Convolution2D(64,3,3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size = (2,2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout('0.2')) model.add(Dense(10)) model.add(Activation('softmax')) model.compile()
import keras from keras.model import Sequential from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout from keras.optimizers import Adam from keras.callbacks import TensorBoard cnn_model = Sequential() cnn_model.add(Conv2D(32, 3, 3, input_shape = (28, 28, 1), activation = 'relu')) cnn_model.add(MaxPooling2D(pool_size = (2,2))) cnn_model.add(Flatter()) cnn_model.add(Dense(output_dim = 32, activation = 'relu')) cnn_model.add(Dense(output_dim = 32, activation = 'sigmoid')) cnn_model.compile(loss ='asparse_categorical_crossentropy', optimizersadam(lr=0.001), matrics =['accuracy']) epochs = 50 cnn_model.fix(X_train, y_train,batch_size = 512, nb_epoch = epochs, verbose = 1, validation_data = (X_validate, y_validate)) #evaluating the model evaluation = cnn_model.evaluate(X_test, y_test) print('Test Accuracy : {:.3f}'.format(evaluation[1])) predicted_classes = cnn_model.predict_classes(X_test) predicted_classes L = 5 W = 5 fig, axes = plt.subplots(L, W, figsize = (12,12)) axes = axes.ravel() for i in np.arange(0, L * W):
from keras.layers import Dense from keras.optimizers import Adam,SGD %matplotlib inline import matplotlib.pyplot as plt import numpy as np import pandas as pd df = pd.read_csv('../data/weight-height.csv') X = df[['Height']].values y_true = df[['Weight']].values model = Sequential() #model.add(Dense(1,input_shape(1,)) model.add(Dense(4,input_shape(1,)) model.add(Dense(4,input_shape(4,)) model.add(Dense(1,input_shape(4,)) model.compile(Adam(lr=0.8),'mean_squared_error') model.fit(X,y_true,epochs=40) ''' performance of model Classification Linear Regression'''
loss='hinge') siamese.set_weight(siamese_theano) get_feature100 = theano.function(siamese.layers[0].input, siamese.layers[-2].output) feat_x = get_feature100(train_x) knn = KNeighborsClassifier(n_neighbors=5) knn.fit(feat_x) feat_neighbor = np.empty((n_samples, 6, feat_x.shape[1])) for sample in xrange(n_samples): neighbor_x = knn.kneighbors(feat_x[sample]) feat_neighbor[sample, :5, :] = neighbor_x feat_neighbor[sample, 5, :] = feat_x[sample] rnn = Sequential() rnn.add(LSTM(100, input_shape=(5, 100), activation='tanh')) rnn.add(Dense(n_labels, activation='softmax')) rnn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) rnn.fit(feat_neighbor, train_y, batch_size=5, nb_epoch=10, verbose=1)
x_train = np.array(images) y_train = np.array(measurements) from keras.model import Sequential from keras.layers import Flatten, dense, Lambda model = Sequential() # lambda layer for normalization model.add(Lambda(Lambda x: x / 255.0, input_shape = (160, 320, 3))) model.add(Flatten()) model.add(Dense(1)) model.compile(loss="mse", optimizer="adam") model.fit(x_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=5)
classifier = Sequential() classifier.add( Convolution2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu') ) #32,3,3 is number of filters having 3X3 size and Input_shape 64X64 for RGB(3) classifier.add(MaxPooling2D(pool_size(2, 2))) classifier.add( Convolution2D(32, 3, 3, activation='relu') ) #32,3,3 is number of filters having 3X3 size and Input_shape 64X64 for RGB(3) classifier.add(MaxPooling2D(pool_size(2, 2))) classifier.add(Dense(output_dim=128, activation='relu')) classifier.add(Dense(output_dim=1, activation='sigmoid')) #compliling classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) from keras.preprosessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) training_set = train_datagen.flow_from_directory('dataset/training_set', target_size=(64, 64), batch_size=32, class_mode='binary')