def GLM_network_fit(stimulus, spikes, d_stim, d_spk, bin_len, f='exp', priors=None, L1=None): N = spikes.shape[0] M = stimulus.shape[0] F = np.empty((N, M, d_stim)) # stimulus filters W = np.empty((N, N, d_spk)) # spike train filters b = np.empty((N, )) # biases fs = {'exp': K.exp} Xdsn = construct_GLM_mat(stimulus, spikes, d_stim, d_spk) for i in range(N): y = spikes[i, max(d_stim, d_spk):] # construct GLM model and return fit model = Sequential() model.add(Dense(1, input_dim=Xdsn.shape[1], use_bias=True)) model.add(Lambda(lambda x: fs[f](x) * bin_len)) model.compile(loss='poisson', optimizer=keras.optimizers.adam(lr=2e-1)) model.fit(x=Xdsn, y=y, epochs=150, batch_size=1000000, verbose=0) p = model.get_weights()[0] F[i, :, :] = p[:M * d_stim].reshape((M, d_stim)) W[i, :, :] = p[M * d_stim:].reshape((N, d_spk)) b[i] = model.get_weights()[1] return (F, W, b)
def train_model(feature: Series, label: Series, learning_rate, number_epochs, batch_size): # Create model model = Sequential() model.add(Dense(units=1, activation='relu', input_shape=(1, ))) model.compile(optimizer=RMSprop(lr=learning_rate), loss='mean_squared_error', metrics=[RootMeanSquaredError()]) # Train model model_hist = model.fit(x=feature, y=label, epochs=number_epochs, batch_size=batch_size) # Get root mean squared error and epoch data df_hist = DataFrame(model_hist.history) root_mean_squared_error = df_hist['root_mean_squared_error'] # Get weight, bias weights = model.get_weights() weight = weights[0] bias = weights[1] return weight, bias, root_mean_squared_error, model_hist.epoch
class DQTModel: def __init__(self, input_size, lr, hidden_layers, seed=None): self.ann = Sequential() self.ann.add( Dense(hidden_layers[0], activation='relu', input_dim=input_size)) for units in hidden_layers[1:]: self.ann.add(Dense(units, activation='relu')) self.ann.add(Dense(3, activation='linear')) self.ann.compile(optimizer=Adam(lr=lr), loss='mse') def _huber_loss(self, target, prediction): error = prediction - target return K.mean(K.sqrt(1 + K.square(error)) - 1, axis=-1) def train(self, batch): self.ann.fit(batch["inputs"], batch["targets"], steps_per_epoch=1, epochs=1, verbose=0) def predict(self, input): input = input.reshape([1, len(input)]) return self.ann.predict(input, steps=1, verbose=0)[0] def interpolate(self, model, factor): mine = self.ann.get_weights() other = model.ann.get_weights() for i, o in enumerate(other): mine[i] = o * factor + (1 - factor) * mine[i] self.ann.set_weights(mine)
def test_one_layer_relu_activation_none_regularization_mse(self): np.random.seed(42) actual = mydnn([ { 'input': 2, 'output': 1, 'nonlinear': 'relu', 'regularization': 'l2', } ], 'MSE') x = np.random.normal(size=(3, 2)) y = np.random.normal(size=(3,1)) w_before = actual._architecture[0]._w._value.copy() def kernel_initializer(shape, dtype=None): w = w_before assert w.shape == shape return w b_before = actual._architecture[0]._b._value.copy() def bias_initializer(shape, dtype=None): b = b_before assert b.shape == shape return b sgd = optimizers.SGD() desired = Sequential() desired.add(layers.Dense(1, activation='relu', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, input_shape=(x.shape[1],))) desired.compile(sgd, 'MSE') desired.fit(x, y, batch_size=x.shape[0]) actual.fit(x, y, 1, x.shape[0], 0.01) w_desired, b_desired = desired.get_weights() w_actual = actual._architecture[0]._w._value b_actual = actual._architecture[0]._b._value np.random.seed(42) #dl_db_desired = 2.0 * np.sum(x @ w_before + np.full((3,1), b_before) - y) / x.shape[0] dl_db_actual1 = actual._architecture[0]._b.get_gradient() dl_db_actual2 = (b_before - b_actual) / 0.01 # np.testing.assert_allclose(dl_db_actual1, dl_db_desired) # np.testing.assert_allclose(dl_db_actual2, dl_db_desired) np.testing.assert_allclose(b_actual, b_desired) np.testing.assert_allclose(w_actual, w_desired)
def test_one_layer_none_activation_l2_regularization_mse(self): np.random.seed(42) weight_decay = 10.0 actual = mydnn([ { 'input': 2, 'output': 1, 'nonlinear': 'none', 'regularization': 'l2', } ], 'MSE', weight_decay=weight_decay) x = np.arange(6).reshape(3, 2) y = np.array([[6], [7], [8]]) w_before = actual._architecture[0]._w._value.copy() def kernel_initializer(shape, dtype=None): w = w_before assert w.shape == shape return w b_before = actual._architecture[0]._b._value.copy() def bias_initializer(shape, dtype=None): b = b_before assert b.shape == shape return b sgd = optimizers.SGD() desired = Sequential() desired.add(layers.Dense(1, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, input_shape=(x.shape[1],), kernel_regularizer=regularizers.l2(weight_decay))) desired.compile(sgd, 'MSE') desired.fit(x, y, batch_size=x.shape[0]) actual.fit(x, y, 1, x.shape[0], 0.01) w_desired, b_desired = desired.get_weights() w_actual = actual._architecture[0]._w._value b_actual = actual._architecture[0]._b._value dl_dw_desired2 = 2.0 * (np.transpose(x) @ (x @ w_before + b_before - y) / x.shape[0] + weight_decay * w_before) dl_dw_desired1 = (w_before - w_desired) / 0.01 dl_dw_actual1 = (w_before - w_actual) / 0.01 np.testing.assert_allclose(dl_dw_actual1, dl_dw_desired1, atol=1e-5) np.testing.assert_allclose(dl_dw_actual1, dl_dw_desired2, atol=1e-5) np.testing.assert_allclose(b_actual, b_desired) np.testing.assert_allclose(w_actual, w_desired, atol=1e-5)
def test_one_layer_sigmoid_activation_none_regularization_mse(self): np.random.seed(42) actual = mydnn([ { 'input': 2, 'output': 1, 'nonlinear': 'sigmoid', 'regularization': 'l2', } ], 'MSE') x = np.random.normal(size=(3, 2)) y = np.random.normal(size=(3,1)) w_before = actual._architecture[0]._w._value.copy() def kernel_initializer(shape, dtype=None): w = w_before assert w.shape == shape return w b_before = actual._architecture[0]._b._value.copy() def bias_initializer(shape, dtype=None): b = b_before assert b.shape == shape return b sgd = optimizers.SGD() desired = Sequential() desired.add(layers.Dense(1, activation='sigmoid', kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, input_shape=(x.shape[1],))) desired.compile(sgd, 'MSE') desired.fit(x, y, batch_size=x.shape[0]) actual.fit(x, y, 1, x.shape[0], 0.01) w_desired, b_desired = desired.get_weights() w_actual = actual._architecture[0]._w._value b_actual = actual._architecture[0]._b._value np.random.seed(42) np.testing.assert_allclose(b_actual, b_desired, rtol=1e-7, atol=1e-9) np.testing.assert_allclose(w_actual, w_desired, rtol=1e-7, atol=1e-9)
def keras_regressor(X, y, N, lam, num_epochs, bsize, learn_rate): model = Sequential() model.add( Dense(1, input_dim=X.shape[1], use_bias=False, kernel_regularizer=regularizers.l2(lam))) model.compile(loss='mean_squared_error', optimizer=keras.optimizers.adam(lr=learn_rate)) history = model.fit(x=X, y=y, epochs=num_epochs, batch_size=bsize, verbose=2) p = model.get_weights()[0] return (history, p)
patience=2, verbose=1, mode='auto') save_model = ModelCheckpoint(os.path.join(path, "model.h5"), monitor='val_loss', save_best_only=True) history = model.fit(x=X_train, y=y_train1, verbose=3, epochs=100, batch_size=32, validation_split=0.1, callbacks=[early_stopping, save_model]) print(model.summary()) print(model.get_weights()) print(history.history['accuracy'][-1]) print(history.history['val_accuracy'][-1]) kutils.plot_loss(history) digit_test = pd.read_csv(os.path.join(path, "test.csv")) digit_test.shape digit_test.info() X_test = digit_test / 255.0 pred = model.predict_classes(X_test) submissions = pd.DataFrame({ "ImageId": list(range(1, len(pred) + 1)), "Label": pred })
class Model(): def __init__(self, logging=False): self.logging = logging # Initial standard deviation of the kernel self.kernel_init_std = 1 self.kernel_weights = None self.k_size = 8 self.rnn_timesteps = 4 self.rnn_layers = [128, 128] self.rnn_activation = 'relu' self.with_zoom = False self.control_output = 3 if self.with_zoom else 2 self.batch_size = 64 self.path_to_images = "data/mnist/mnist.pkl" self.image_size = 28 self.nr_of_classes = 10 # gpu_options = K.tf.GPUOptions(per_process_gpu_memory_fraction=0.333) config = K.tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = K.tf.Session(config=config) with self.sess.as_default(): self.glimpse = K.variable(np.zeros((1, 1, self.k_size**2))) self.init_image_loader() self.init_networks() self.init_weight_sizes() def init_weight_sizes(self): # 3 because we need x, y coordinates and std in both directions self.kernel_weight_size = 3 * self.k_size**2 self.rnn_weight_size = sum( [w.size for w in self.rnn_model.get_weights()]) self.control_weight_size = sum( [w.size for w in self.control_model.get_weights()]) self.classifier_weight_size = sum( [w.size for w in self.classifier_model.get_weights()]) self.weights_size = self.kernel_weight_size + self.rnn_weight_size + self.control_weight_size + self.classifier_weight_size def init_networks(self): self.rnn_model = Sequential() self.rnn_model.add( SimpleRNN(self.rnn_layers[0], activation=self.rnn_activation, input_shape=(self.rnn_timesteps, self.k_size**2), return_sequences=True)) self.rnn_model.add( SimpleRNN(self.rnn_layers[1], activation=self.rnn_activation)) # self.rnn_model.summary() self.control_model = Sequential( [Dense(units=self.control_output, input_dim=self.rnn_layers[-1])]) # self.control_model.summary() self.classifier_model = Sequential( [Dense(units=self.nr_of_classes, input_dim=self.rnn_layers[-1])]) # self.classifier_model.summary() self.sess.run(K.tf.global_variables_initializer()) def init_image_loader(self): self.train_x, self.train_y, self.test_x, self.test_y = mnist_loader.load( self.path_to_images) middle = math.sqrt(len(self.train_x[0])) / 2 self.lattice = [middle, middle] # If init_kernel, we ignore the input and set the kernel positions def set_weights(self, weights): with self.sess.as_default(): k, r, co, cl = self.kernel_weight_size, self.rnn_weight_size, self.control_weight_size, self.classifier_weight_size # 3, because x, y, std self.kernel_weights = np.reshape(weights[:k], (3, -1)) w1 = k self.rnn_weights = [] for w in self.rnn_model.get_weights(): self.rnn_weights.append( np.reshape(weights[w1:w1 + w.size], w.shape)) w1 += w.size self.control_weights = [] for w in self.control_model.get_weights(): self.control_weights.append( np.reshape(weights[w1:w1 + w.size], w.shape)) w1 += w.size self.classifier_weights = [] for w in self.classifier_model.get_weights(): self.classifier_weights.append( np.reshape(weights[w1:w1 + w.size], w.shape)) w1 += w.size self.rnn_model.set_weights(self.rnn_weights) self.control_model.set_weights(self.control_weights) self.classifier_model.set_weights(self.classifier_weights) def set_batch_size(self, batch_size): self.batch_size = batch_size def get_weights_size(self): return self.weights_size def set_logging(self, logging): self.logging = logging def classify(self, features): pass def get_score(self): return self.accuracy def train(self, epoch=None): true_positives = 0 with self.sess.as_default(): indices = range( (epoch * self.batch_size) % len(self.train_x), ((epoch + 1) * self.batch_size) % len(self.train_x)) for i in indices: img = self.train_x[i] for n in range(self.rnn_timesteps): k = self.kernel_weights glimpse_ = GlimpseGenerator().get_glimpse( img, self.lattice[0], self.lattice[1], k[0], k[1], k[2]) # print("Glimpse:") # print(glimpse_) K.set_value(self.glimpse, glimpse_.reshape((1, 1, self.k_size**2))) # Get the RNN params to feed to control or classifier network rnn_out = self.rnn_model.call(self.glimpse) # print("RNN weights:") # print(rnn_out.eval()) control_out = self.control_model.call(rnn_out) # print(type(control_out)) control_out = control_out.eval() class_out = self.classifier_model.call(rnn_out).eval() self.lattice[0] = control_out[0][0] self.lattice[1] = control_out[0][1] # print(class_out) # print(control_out) true_positives += np.argmax(class_out) == self.train_y[i] # K.clear_session() # TODO - simplest scoring right now - we probably want to change this to reward guessing quicker self.accuracy = true_positives / (self.batch_size * self.rnn_timesteps) # print("acc: {}".format(self.accuracy)) def test(self): pass def visualize(self, epoch, res_directory=None, filename=None): scale = 20 img = np.zeros((scale * self.image_size, scale * self.image_size, 3), np.uint8) for i in self.kernel_weights.T: img = cv2.circle(img, (int((self.image_size / 2 - int(i[0])) * scale), int((self.image_size / 2 - int(i[1])) * scale)), abs(int(i[2] * scale)), (0, 0, 255), -1) if filename is None: filename = res_directory + "lattice-epoch_{}-{}.png".format( epoch, str(time.time())[-5:]) cv2.imwrite(filename, img)
x_range = np.array([1]) model.add( Dense(first_layer_nur, input_dim=1, weights=list([weights_arr, bias_arr]), activation='linear')) model.compile(loss='mean_squared_error', optimizer=SGD(lr=max_lr), metrics=['mae']) for i in np.arange(1, epochs, 1, dtype=float): model.fit(x_train, y_train, batch_size=batch_size, epochs=1, verbose=0) weights_arr = np.append(weights_arr, model.get_weights()[0]) bias_arr = np.append(bias_arr, model.get_weights()[1]) x_range = np.append(x_range, i) plt.plot(x_range, weights_arr, '.') plt.plot(x_range, bias_arr, '.') plt.title('learning rate = %-0.2f %s' % (max_lr, titles[title_i])) plt.legend(('weights', 'biases'), loc='upper left', shadow=True) plt.show() plt.close() title_i += 1
with open(file,'a') as outfile: outfile.write('+++++++++++++++++++++++++++++++++++++++\n') outfile.write(label) outfile.write('\n+++++++++++++++++++++++++++++++++++++++\n') loss = [] #Iterate through each training duration for ind,epochs in enumerate(epochs_list): #Fit the model for the current number of epochs history = model.fit(X, y, epochs=epochs, batch_size=batch_size) loss.extend(history.history['loss']) # print(history.history['loss']) #Extract the weights from the training model to set the weights for the #character generation model weights = model.get_weights() trained_model.set_weights(weights) #Select a sample from X to be the seed seed = np.array(X[100]) #Generate the results and output to file with open(file,'a') as outfile: #Write the header line with number of epochs, seed, etc outfile.write('Using seed of length {} generated {} characters after {} epochs.\nSeed: \'{}\'\n'.format( seed.shape[0], num_to_generate, sum(epochs_list[:ind+1]), ''.join(onehot_to_char(seed)))) #Generate the characters from the current iteration of the trained model chars = generate_text(trained_model,num_to_generate,seed)
class GAN: def __init__(self): self.batch_size = 32 self.log_step = 50 self.scaler = MinMaxScaler((-1, 1)) self.data = self.get_data_banknotes() self.init_model() # Logging loss self.logs_loss = pd.DataFrame(columns=['d_train_r', # real data from discriminator training 'd_train_f', # fake data from discriminator training 'd_test_r', # real data from discriminator testing 'd_test_f', # fake data from discriminator testing 'a' # data from GAN(adversarial) training ]) # Logging accuracy self.logs_acc = pd.DataFrame(columns=['d_train_r', 'd_train_f', 'd_test_r', 'd_test_f', 'a']) # Logging generated rows self.results = pd.DataFrame(columns=['iteration','variance', 'skewness', 'curtosis', 'entropy', 'prediction']) def get_data_banknotes(self): """ Get data from file :return: """ names = ['variance', 'skewness', 'curtosis', 'entropy', 'class'] dataset = pd.read_csv('data/data_banknotes.csv', names=names) dataset = dataset.loc[dataset['class'] == 0].values # only real banknotes, because fake ones will be generated X = dataset[:, :4] # omitting last column, we already know it will be 0 data = self.structure_data(X) return data def scale(self, X): return self.scaler.fit_transform(X) def descale(self, X): return self.scaler.inverse_transform(X) def structure_data(self, X): """ Structure data :param X: :return: """ data_subsets = {'normal': X, 'scaled': self.scale(X)} for subset, data in data_subsets.items(): # splitting each subset on train and test splited_data = train_test_split(data, test_size=0.3, shuffle=True) data_subsets.update({ subset: { 'train': splited_data[0], 'test': splited_data[1]} }) return data_subsets def init_discriminator(self): """ Init trainable discriminator model. Will be used for training and testing itself outside connected GAN model. LeakyReLU activation function, Adam optimizer and Dropout are recommended in GAN papers """ self.D = Sequential() self.D.add(Dense(16, input_dim=4)) self.D.add(LeakyReLU()) self.D.add(Dropout(0.3)) self.D.add(Dense(16)) self.D.add(LeakyReLU()) self.D.add(Dense(16)) self.D.add(LeakyReLU()) self.D.add(Dense(1, activation='sigmoid')) self.D.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) def init_discriminator_G(self): """ Init non-trainable discriminator model. Will be used for training generator inside connected GAN model. LeakyReLU activation function, Adam optimizer and Dropout are recommended in GAN papers """ self.Dg = Sequential() self.Dg.add(Dense(16, input_dim=4)) # activation function: ganhacks self.Dg.add(LeakyReLU()) self.Dg.add(Dropout(0.3)) self.Dg.add(Dense(16)) self.Dg.add(LeakyReLU()) self.Dg.add(Dense(16)) self.Dg.add(LeakyReLU()) # activation function: ganhacks self.Dg.add(Dense(1, activation='sigmoid')) self.Dg.trainable = False self.Dg.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) def init_generator(self): """ LeakyReLU activation function, Adam optimizer and Dropout are recommended in GAN papers for BOTH D and G """ self.G = Sequential() self.G.add(Dense(16, input_dim=64)) self.G.add(LeakyReLU()) self.G.add(Dropout(0.3)) self.G.add(Dense(16)) self.G.add(LeakyReLU()) self.G.add(GaussianNoise(0.1)) self.G.add(Dense(16)) self.G.add(LeakyReLU()) self.G.add(Dense(4, activation='tanh')) self.G.compile(loss='binary_crossentropy', optimizer='adam') def init_model(self): """ Connecting non trainable model with Generator. Initializing D. :return: """ self.init_discriminator() self.init_discriminator_G() self.init_generator() self.GAN = Sequential() self.GAN.add(self.G) self.GAN.add(self.Dg) self.GAN.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) def get_adversarial_data(self, mode='train'): """ Get data for adversarial training. """ data = self.data['scaled'][mode].copy() np.random.shuffle(data) features_real = data[:int(self.batch_size / 2)] # random rows with real data noise = np.random.uniform(-1.0, 1.0, size=[int(self.batch_size / 2), 64]) # random noise for generator features_fake = self.G.predict(noise) # fake data y_real = np.zeros([int(self.batch_size / 2), 1]) # array of zeros for real rows labels y_fake = np.ones([int(self.batch_size / 2), 1]) # array of ones for fake rows labels return features_real, y_real, features_fake, y_fake def train(self, train_steps): try: for i in range(train_steps): # Training D xr, yr, xf, yf = self.get_adversarial_data() # train D separately from G d_loss_r = self.D.train_on_batch(xr, yr) # separating real and fake data is recommended d_loss_f = self.D.train_on_batch(xf, yf) # Training G # flipping the label before prediction will # not influence D prediction as here D is not trainable and is getting weights from trainable D y = np.zeros([int(self.batch_size / 2), 1]) # flipping labels is recommended self.Dg.set_weights(self.D.get_weights()) # Copying weights from trainable D noise = np.random.uniform(-1.0, 1.0, size=[int(self.batch_size / 2), 64]) # getting input noise for G a_loss = self.GAN.train_on_batch(noise, y) # Testing xr_t, yr_t, xf_t, yf_t = self.get_adversarial_data(mode='test') d_pred_r = self.D.predict_on_batch(xr_t) # getting example predictions d_pred_f = self.D.predict_on_batch(xf_t) d_loss_r_t = self.D.test_on_batch(xr_t, yr_t) # getting loss and acc d_loss_f_t = self.D.test_on_batch(xf_t, yf_t) # Logging important data self.log(locals()) finally: """ Plot and save data when finished. """ self.plot() self.results.to_csv('results/results.csv', index=False) def plot(self): """ Preparing for plotting, plotting and saving plots. """ import matplotlib.pyplot as plt ax_loss = self.logs_loss.plot(linewidth=0.75, figsize=(20, 10)) ax_loss.set_xlabel('iteration') ax_loss.set_ylabel('loss') fig = plt.gcf() fig.set_dpi(200) plt.legend(loc='upper right', framealpha=0, prop={'size': 'large'}) fig.savefig('results/loss.png', dpi=200) ax_acc = self.logs_acc.plot(linewidth=0.75, figsize=(20, 10)) ax_acc.set_xlabel('iteration') ax_acc.set_ylabel('accuracy') fig = plt.gcf() fig.set_dpi(200) plt.legend(loc='upper right', framealpha=0, prop={'size': 'large'}) fig.savefig('results/acc.png', dpi=200) plt.show() def log(self, variables): """ Logging and printing all the necessary data """ r_rows = pd.DataFrame(self.descale(variables['xr_t']), columns=['variance', 'skewness', 'curtosis', 'entropy']) r_rows['prediction'] = variables['d_pred_r'] f_rows = pd.DataFrame(self.descale(variables['xf_t']), columns=['variance', 'skewness', 'curtosis', 'entropy']) f_rows['prediction'] = variables['d_pred_f'] f_rows['iteration'] = variables['i'] self.logs_loss = self.logs_loss.append(pd.Series( # logging loss [variables['d_loss_r'][0], variables['d_loss_f'][0], variables['d_loss_r_t'][0], variables['d_loss_f_t'][0], variables['a_loss'][0]], index=self.logs_loss.columns), ignore_index=True) self.logs_acc = self.logs_acc.append(pd.Series( # logging acc [variables['d_loss_r'][1], variables['d_loss_f'][1], variables['d_loss_r_t'][1], variables['d_loss_f_t'][1], variables['a_loss'][1]], index=self.logs_loss.columns), ignore_index=True) self.results = self.results.append(f_rows, ignore_index=True, sort=False) # logging generated data if self.log_step and variables['i'] % self.log_step == 0: # print metrics every 'log_step' iteration # preparing strings for printing log_msg = f""" Batch {variables['i']}: D(training): loss: real : {variables['d_loss_r'][0]:.4f} fake : {variables['d_loss_f'][0]:.4f} acc: real: {variables['d_loss_r'][1]:.4f} fake: {variables['d_loss_f'][1]:.4f} D(testing): loss: real : {variables['d_loss_r_t'][0]:.4f} fake : {variables['d_loss_f_t'][0]:.4f} acc: real: {variables['d_loss_r_t'][1]:.4f} fake: {variables['d_loss_f_t'][1]:.4f} GAN: loss: {variables['a_loss'][0]:.4f} acc: {variables['a_loss'][1]:.4f} """ print(log_msg) np.set_printoptions(precision=5, linewidth=140, suppress=True) # set how np.array will be printed predictions = f""" Example results: Real rows: {r_rows} Fake rows: {f_rows} """ print(predictions)
class DQN(): GAMMA = 0.9 LEARNING_RATE = 0.5 BATCH_SIZE = 15 NUM_EPISODES = 50000 def __init__(self, observation_space): self.replay_buffer = Replay_buffer() self.explorationRate = 0.5 self.decay = 0.0002 self.minExplorationRate = 0.0001 self.observation_space = observation_space self.model = Sequential([ Dense(15, input_shape=(observation_space, )), Activation("relu"), Dense(6), Activation("relu"), Dense(2) ]) self.model.compile(loss="mse", optimizer=Adam(lr=DQN.LEARNING_RATE)) self.critic = Sequential([ Dense(15, input_shape=(observation_space, )), Activation("relu"), Dense(6), Activation("relu"), Dense(2) ]) self.critic.compile(loss="mse", optimizer=Adam(lr=DQN.LEARNING_RATE)) def decay_epsilon(self, episodes): if (episodes < 0.6 * DQN.NUM_EPISODES): self.decay = 1 / (2 * DQN.NUM_EPISODES) else: self.decay = 10 / DQN.NUM_EPISODES self.explorationRate = self.minExplorationRate + ( self.explorationRate - self.minExplorationRate) * (np.exp(-self.decay)) def policy(self, state): x = random.random() if (x < self.explorationRate): return random.randint(0, 1) # state2 = np.asarray(state) # state2 = state2.reshape([1, self.observation_space]) # print(state.shape) q_values = self.model.predict(state) return np.argmax(q_values[0]) def train(self, num_ep): batch = self.replay_buffer.sample(DQN.BATCH_SIZE) for state, action, reward, state_next, terminal in batch: q_update = reward if not terminal: q_update += DQN.GAMMA * np.amax( self.critic.predict(state_next)[0]) q_values = self.model.predict(state) # print(q_values) q_values[0][action] = q_update self.model.fit(state, q_values, verbose=0) self.decay_epsilon(num_ep) def make_equal(self): self.critic.set_weights(self.model.get_weights())
model.add(Dense(2, input_dim=2, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.1)) print(model.summary()) # Training X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) model.fit(X, y, batch_size=1, epochs=1000, verbose=0) # Result print("Network test:") print("XOR(0,0):", model.predict_proba(np.array([[0, 0]]))) print("XOR(0,1):", model.predict_proba(np.array([[0, 1]]))) print("XOR(1,0):", model.predict_proba(np.array([[1, 0]]))) print("XOR(1,1):", model.predict_proba(np.array([[1, 1]]))) # Parameters layer 1 W1 = model.get_weights()[0] b1 = model.get_weights()[1] # Parameters layer 2 W2 = model.get_weights()[2] b2 = model.get_weights()[3] print("W1:", W1) print("b1:", b1) print("W2:", W2) print("b2:", b2) print()
def _compare_models(architecture, loss, weight_decay=0.0, number_of_samples=3, learning_rate=0.01, epochs=1): np.random.seed(42) x = np.random.rand(number_of_samples, architecture[0]['input']) if 'MSE' == loss: y = np.random.rand(number_of_samples, 1) keras_loss = 'MSE' keras_metrics = None elif 'cross-entropy' == loss: number_of_classes = architecture[-1]['output'] y = np.eye(number_of_classes)[np.random.choice(number_of_classes, size=number_of_samples)] keras_loss = losses.categorical_crossentropy keras_metrics = [metrics.categorical_accuracy] else: assert False, loss actual = mydnn(architecture, loss, weight_decay) sgd = optimizers.SGD(lr=learning_rate) desired = Sequential() for index, layer in enumerate(architecture): units = layer['output'] activation = layer['nonlinear'] if 'sot-max' == activation: activation = 'softmax' def kernel_initializer(shape, dtype=None): w = actual._architecture[index]._w.get_value() assert w.shape == shape return w def bias_initializer(shape, dtype=None): b = actual._architecture[index]._b.get_value() assert b.shape == shape return b if 'l1' == layer['regularization']: kernel_regularizer = regularizers.l1(weight_decay) elif 'l2' == layer['regularization']: kernel_regularizer = regularizers.l2(weight_decay) else: assert False desired.add(layers.Dense(units, activation=activation, kernel_initializer=kernel_initializer, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, input_shape=(layer['input'],))) desired.compile(sgd, keras_loss, metrics=keras_metrics) desired.fit(x, y, batch_size=x.shape[0], epochs=1) actual.fit(x, y, epochs, x.shape[0], learning_rate) weights_and_biases_desired = desired.get_weights() weights_and_biases_actual = list() for index in range(len(architecture)): weights_and_biases_actual.append(actual._architecture[index]._w.get_value()) weights_and_biases_actual.append(actual._architecture[index]._b.get_value()) for weight_actual, weight_desired in zip(weights_and_biases_actual, weights_and_biases_desired): np.testing.assert_allclose(weight_actual, weight_desired, atol=1e-5) y_predicted_actual = actual.predict(x, batch_size=number_of_samples) y_predicted_desired = desired.predict(x, batch_size=number_of_samples) np.testing.assert_allclose(y_predicted_actual, y_predicted_desired) if 'MSE' == loss: loss_actual = actual.evaluate(x, y, batch_size=number_of_samples) loss_desired = desired.evaluate(x, y, batch_size=number_of_samples) np.testing.assert_allclose(loss_actual, loss_desired) else: loss_actual, accuracy_actual = actual.evaluate(x, y, batch_size=number_of_samples) loss_desired, accuracy_desired = desired.evaluate(x, y, batch_size=number_of_samples) np.testing.assert_allclose(loss_actual, loss_desired) np.testing.assert_allclose(accuracy_actual, accuracy_desired)
x = np.linspace(-1, 1, 2000) np.random.shuffle(x) y = 0.5 * x + 2 + np.random.normal(0, 0.5, (2000,)) # prepare test data and training data x_train, y_train = x[:1600], y[:1600] x_test, y_test = x[1600:], y[1600:] # setup network model = Sequential() model.add(Dense(1,input_dim=1)) model.compile(loss='mse', optimizer="sgd") # training model print("Training") for step in range(3001): cost = model.train_on_batch(x_train, y_train) if step % 100 == 0: print("training cost : ", cost) print("Testing") cost = model.evaluate(x_test, y_test, batch_size=40) print("test cost", cost) w, b = model.get_weights() print("weight = ", w, "bias", b) Y_pred = model.predict(x_test) plt.scatter(x_test,y_test) plt.plot(x_test,Y_pred) plt.show()
plt.close() return 0 if __name__ == '__main__': count = 1000 input_dim = 1 seed = 13 model = Sequential() model.add( Dense(count, input_dim=input_dim, kernel_initializer=wi.Zeros(), bias_initializer=wi.Zeros())) plot_weights(weights=model.get_weights(), x=np.arange(0, count, 1), title='Zeros') model = Sequential() model.add( Dense(count, input_dim=input_dim, kernel_initializer=wi.Ones(), bias_initializer=wi.Ones())) plot_weights(weights=model.get_weights(), x=np.arange(0, count, 1), title='Ones') model = Sequential() model.add(
class DeepNeuralClassifier(BaseClassifier): encoder = LabelBinarizer( ) # please annotate what this is doing (hypothesis: for to_categorical()) def __init__(self, feature_length, num_classes): super().__init__(feature_length, num_classes) self.num_classes = num_classes # From Keras examples (https://keras.io/getting-started/sequential-model-guide/) self.model = Sequential() self.model.add(Dense(64, activation='relu', input_dim=feature_length)) self.model.add(Dropout(0.5)) self.model.add(Dense(64, activation='relu')) self.model.add(Dropout(0.5)) self.model.add(Dense(num_classes, activation='softmax')) self.model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) self.initial_weights = self.model.get_weights() def train(self, features, labels): """ Using a set of features and labels, trains the classifier and returns the training accuracy. :param features: An MxN matrix of features to use in prediction :param labels: An M row list of labels to train to predict :return: Prediction accuracy, as a float between 0 and 1 """ labels = self.labels_to_categorical(labels) result = self.model.fit(features, labels, epochs=16, verbose=0) return result.history['acc'][-1] # make sure you save model using the same library as we used in machine learning price-predictor def predict(self, features, labels): """ Using a set of features and labels, predicts the labels from the features, and returns the accuracy of predicted vs actual labels. :param features: An MxN matrix of features to use in prediction :param labels: An M row list of labels to test prediction accuracy on :return: Prediction accuracy, as a float between 0 and 1 """ labels = self.labels_to_categorical(labels) accuracy = self.model.evaluate(features, labels, verbose=0)[1] return accuracy def get_prediction(self, features): ''' this function get the prediction from the :param features: sample to predict :return: prediction from the model ''' probabilities_list = self.model.predict(features) return [ list(probabilities).index(max(list(probabilities))) for probabilities in probabilities_list ] def reset(self): """ Resets the trained weights / parameters to initial state :return: """ self.model.set_weights(self.initial_weights) pass def labels_to_categorical(self, labels): ''' convert the labels from string to number :param labels: labels list of string :return: labels converted in number ''' _, IDs = unique(labels, return_inverse=True) return to_categorical(IDs, num_classes=self.num_classes)
class NeuralNetwork(): def __init__(self): self._model = None self._callbacks = [] def createModel(self, input_data, output_data): """ Input and Output Data can be single or multi data (lists)""" return models.Model(inputs=input_data, outputs=output_data) def createSequentialModel(self): """ Create Empty Sequential Model, additional Layers are required""" try: self._model = Sequential() except Exception as n: print("Creating Sequential Model failed") print(n) def getCallbacks(self): return self._callbacks def getModel(self): return self._model def add(self, layer): try: self._model.add(layer) except Exception as e: print("Failed to add layer!") print(e) def getWeights(self): """ Return Weights""" try: return self._model.get_weights() except Exception as n: print(n) def fit(self, _x, _y, _batch_size=None, _epochs=1, _verbose=1, _validation_split=None): """ Fit the given Model""" """ _x = networkinput _y = networkoutput """ #try: return self._model.fit(x=_x, y=_y, batch_size=_batch_size, epochs=_epochs, verbose=_verbose, callbacks=self._callbacks, validation_split=_validation_split) #except Exception as n: # print("Fit Model Failed!") #print(n) #return None def compile(self, _path=None, _optimizer=None, _loss=None, _metrics=None, _callbacks=[]): """ Compile the given Model """ """ Loss Functions: mean_squared_error mean_absolute_error mean_absolute_percentage_error mean_squared_logarithmic_error squared_hinge hinge categorical_hinge logcosh Optimizers: SGD RMSprop Adadelta Adam Adamax Nadam TFOptimizer Metrics: binary_accuracy categorical_accuracy sparse_categorical_accuracy top_k_categorical_accuracy sparse_top_k_categorical_accuracy """ try: self._model.compile(optimizer=_optimizer, loss=_loss, metrics=_metrics) if not _path is None: filepath = _path + "/weights-improvement-{epoch:02d}-{loss:.4f}-bigger.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=0, save_best_only=True, mode='auto') self._callbacks.append(checkpoint) # append additional callbacks if not _callbacks is None: for callback in _callbacks: self._callbacks.append(callback) except Exception as n: print("Compiling Model Failed!") print(n) def evaluate(self, _x, _y, _batch_size=None, _verbose=1, _sample_weight=None, _steps=None): """ Evaluate the Model""" try: scores = self._model.evaluate(_x, _y, batch_size=_batch_size, verbose=_verbose, sample_weight=_sample_weight, steps=_steps) return "\n%s: %.2f%%".format(self._model.metrics_names[1], scores[1] * 100) except Exception as n: print("Evaluation Failed!") print(n) def predict_Model(self, _x, _batch_size=None, _verbose=0, _steps=None): """ Predict the Model with given Data""" try: x_new = self._model.predict(x=_x, batch_size=_batch_size, verbose=_verbose, steps=_steps) return x_new except Exception as n: print("Prediction Failed") print(n) def plotModel(self, filename): """ Plot the given Model an create an image""" try: return plot_model(self._model, to_file=filename) except Exception as n: print("Unable to plot model!") print(n) def load_weights(self, path): ''' Try to load weights from file. Returns True if successful or False otherwise. ''' try: self._model.load_weights(path) return True except Exception as e: print("Failed to load weights from path: " + path) print(e) return False