print("complie: _train") _train = K.function(train_ins, [train_loss], updates=updates) print("complie: _train_with_acc") _train_with_acc = K.function(train_ins, [train_loss, train_accuracy], updates=updates) print("complie: _predict") _predict = K.function(predict_ins, [y_test], updates=state_updates) print("complie: _test") _test = K.function(test_ins, [test_loss]) print("complie: _test_with_acc") _test_with_acc = K.function(test_ins, [test_loss, test_accuracy]) model = Sequential() model.class_mode = "categorical" model._train = _train model._train_with_acc = _train_with_acc model._predict = _predict model._test = _test model._test_with_acc = _test_with_acc # Train the model each generation and show predictions against the validation dataset for iteration in range(1, 200): print() print("-" * 50) print("Iteration", iteration) model.fit( D_X_train, D_y_train, batch_size=BATCH_SIZE, nb_epoch=1, validation_data=(D_X_val, D_y_val), show_accuracy=True ) ### # Select 10 samples from the validation set at random so we can visualize errors for i in range(10):
X_test, y_test = getTestData() batch_size = 8 nb_epoch = 125 model = Sequential() model.add(Dense(25, input_shape=(13,))) model.add(Activation('linear')) model.add(Dropout(0.10)) model.add(Dense(40)) model.add(Activation('relu')) model.add(Dropout(0.05)) # model.add(Dense(25)) # model.add(Activation('linear')) # model.add(Dropout(0.05)) # this layer doesn't add much model.add(Dense(1)) model.add(Activation('relu')) rms = RMSprop() # ada = Adagrad() model.compile(loss='mean_squared_error', optimizer=rms) model.fit(X, y, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_data=(X_test, y_test)) for i in range(X_test.shape[0]): xn = X_test[i] yn = y_test[i] print yn, model._predict([xn])[0][0]
import numpy as np from keras.layers.convolutional import Convolution1D, MaxPooling1D, Convolution2D, MaxPooling2D from keras.models import Sequential # model = Sequential() # model.add(Convolution1D(input_dim=32, nb_filter=8, filter_length=3, subsample_length=2)) # model.compile(loss='mse', optimizer='sgd') # X = np.random.random((100, 20, 32)) # out = model._predict(X) # print out.shape # model = Sequential() # model.add(Convolution1D(input_dim=32, nb_filter=8, filter_length=3)) # model.add(MaxPooling1D()) # model.compile(loss='mse', optimizer='sgd') # X = np.random.random((100, 20, 32)) # out = model._predict(X) # print out.shape model = Sequential() model.add(Convolution2D(nb_filter=8, stack_size=3, nb_row=3, nb_col=3, border_mode='same')) # model.add(MaxPooling2D()) model.compile(loss='mse', optimizer='sgd') X = np.random.random((20, 3, 32, 1)) out = model._predict(X) print out.shape
model.load_weights('/Users/Aran/Downloads/cross_validation/1000_timestep_chunks/gru_my_weights_iter1000') #loads the training data X_train = pickle.load(open("/Users/Aran/Downloads/cross_validation/1000_timestep_chunks/vsoma_train_x.p","rb")) print (X_train.shape) print (X_train[:3].shape) #shape will be (3, 1000, 2022) #this just picks the first 3000 timesteps (assuming the data #has been processed where each training example consists of 1000 timesteps), which is arbitrarily chosen, #but if you want to change it to just 1000 timesteps simply replace :3 with :1, or if you want to do #2000 timesteps replace :3 with :2, etc. startSeed = X_train[:3] #reshapes it to be a 1 by 3000 (num_timesteps) by 2022 (num_neurons) tensor (Keras requires its inputs to be a tensor). #If you wanted the number of timesteps to be 1000, 2000, 4000 etc, simply change the 3000 to your desired number #of timesteps (has to be a multiple of 1000). seedSeq = np.reshape(startSeed, (1, 3000, 2022)) print (seedSeq.shape) #if we consider at each timestep t as a vector of 2022 (num_neurons) by 1 (representing the voltages of the 2022 neurons at timestep t), #then, for each such vector at any timestep t (where in our case t goes from timestep 0 to 3000), then this predicts the voltages #for timesteps t+1, given the ground truth voltages for timesteps t, t-1, ..., 0. Thus, if t goes from #0 to 3000, then this predicts the voltages of all 2022 neurons from timesteps 1 to 3001, where at any timestep. seedSeqNew = model._predict(seedSeq) print (seedSeqNew.shape) print (seedSeqNew[0].shape) output = seedSeqNew[0] output = output.T print (output.shape) pickle.dump(output, open("gru_output.p", "wb")) #saves the predicted voltages as a 2022 (num_neurons) by num_timesteps array print ('Finished output eval.')
model.add(Dropout(0.15)) # model.add(Dense(30, init='uniform')) # model.add(Activation('sigmoid')) # model.add(Dropout(0.15)) model.add(Dense(2)) model.add(Activation('softmax')) rms = RMSprop() model.compile(loss='binary_crossentropy', optimizer=rms) model.fit(X, y, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_data=(X_test, y_test)) count = 0 for i in range(X_test.shape[0]): ny = 0 if y_test[i][0] > y_test[i][1] else 1 ny_test = model._predict([X_test[i]])[0] ny_test = 0 if ny_test[0] > ny_test[1] else 1 if(ny == ny_test): count += 1 print count / X_test.shape[0] predictions = pd.read_csv('data/test.csv', header=0) predictions['Survived'] = 0 predictions = process(predictions) predictions = predictions[predictions.columns[1:]] predictions = predictions.fillna(predictions.mean()) f = open('data/predictions.csv', 'wb') for index, row in predictions.iterrows(): f.write(str(int(row['PassengerId'])))