Posts

Showing posts with the label neural-network

keras list of Numpy arrays not the size model expected

keras list of Numpy arrays not the size model expected I am having trouble finding the correct way of passing multiple inputs to a model. The model has 2 inputs (256, 256, 3) (256, 256, 3) and 1 output (256, 256, 3) I am producing the images via ImageDataGenerator : ImageDataGenerator x_data_gen = ImageDataGenerator( horizontal_flip=True, validation_split=0.2) And I am producing the samples via a python generator: def image_sampler(datagen, batch_size, subset="training"): for imgs in datagen.flow_from_directory('data/r_cropped', batch_size=batch_size, class_mode=None, seed=1, subset=subset): g_y = noises = bw_images = for i in imgs: # append to expected output the original image g_y.append(i/255.0) noises.append(generate_noise(1, 256, 3)[0]) bw_images.append(iu_rgb2gray(i)) yield(np.array([noises, bw_images]), np.array(g_y)) When trying to train the model with: gene...

Keras shape error when checking input

Keras shape error when checking input I am trying to train a simple MLP model that maps input questions (using a 300D word embedding) and image features extracted using a pretrained VGG16 model to a feature vector of fixed length. However, I can't figure out how to fix the error mentioned below. Here is the code I'm trying to run at the moment: parser = argparse.ArgumentParser() parser.add_argument('-num_hidden_units', type=int, default=1024) parser.add_argument('-num_hidden_layers', type=int, default=3) parser.add_argument('-dropout', type=float, default=0.5) parser.add_argument('-activation', type=str, default='tanh') parser.add_argument('-language_only', type=bool, default= False) parser.add_argument('-num_epochs', type=int, default=10) #default=100 parser.add_argument('-model_save_interval', type=int, default=10) parser.add_argument('-batch_size', type=int, default=128) args = parser.parse_args() questi...

Learning a Sin function

Image
Learning a Sin function I'm new to Machine Learning I' building a simple model that would be able to predict simple sin function sin I generated some sin values, and feeding them into my model. sin from math import sin xs = np.arange(-10, 40, 0.1) squarer = lambda t: sin(t) vfunc = np.vectorize(squarer) ys = vfunc(xs) model= Sequential() model.add(Dense(units=256, input_shape=(1,), activation="tanh")) model.add(Dense(units=256, activation="tanh")) ..a number of layers here model.add(Dense(units=256, activation="tanh")) model.add(Dense(units=1)) model.compile(optimizer="sgd", loss="mse") model.fit(xs, ys, epochs=500, verbose=0) I then generate some test data, which overlays my learning data, but also introduces some new data test_xs = np.arange(-15, 45, 0.01) test_ys = model.predict(test_xs) plt.plot(xs, ys) plt.plot(test_xs, test_ys) Predicted data and learning data looks as follows. The more layers I add, the more curves netw...

Correct way to get output of intermediate layer in Keras model?

Correct way to get output of intermediate layer in Keras model? I have trained a model in Keras and want to extract the output from an intermediate layer. The model contains dropout layers and I want to be absolutely sure nothing is dropped when doing this. According to the documentation, a layer's output can be extracted like this: layer_name = 'my_layer' intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) intermediate_output = intermediate_layer_model.predict(data) However, docs also show how to do so with a Keras function: get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()], [model.layers[3].output]) # output in test mode = 0 layer_output = get_3rd_layer_output([x, 0])[0] # output in train mode = 1 layer_output = get_3rd_layer_output([x, 1])[0] Here, the learning_phase() flag tells keras whether to actually use dropout and similar thin...

pytorch: zero_grad vs zero_grad() - role of parenthesis?

pytorch: zero_grad vs zero_grad() - role of parenthesis? I'm trying to train a pytorch model as follows: start = time.time() for epoch in range(100): t_loss = 0 for i in range(100): optimizer.zero_grad scores = my_model(sent_dict_list[i]) scores = scores.permute(0, 2, 1) loss = loss_function(scores, torch.tensor(targ_list[i]).cuda()) t_loss += loss.item() loss.backward() optimizer.step() print("t_loss = ", t_loss) I find that when I call "optimizer.zero_grad" my loss decreases at the end of every epoch whereas when I call "optimizer.zero_grad()" with the parenthesis it stays almost exactly the same. I don't know what difference this makes and was hoping someone could explain it to me. optimizer.zero_grad is just a function name. you call the function with parenthesis – xssChauhan Jun 28 at 12:10 ...