# Questions tagged [keras]

7022 questions

1

votes

1

answer

662

Views

### keras resume training with different learning rate

I built a simple LSTM model using keras and trained as follows:
model = Sequential()
model.add(LSTM(activation='tanh',input_dim=6,output_dim=50,return_sequences=False))
model.add(Dense(output_dim=1,activation = 'sigmoid'))
model.compile(loss='binary_crossentropy', optimizer =optimizers.Adam(lr = 0.0...

1

votes

1

answer

549

Views

### Python|Keras: how to define a callback to interrupt/exit training per user's request

Currently, I can 'safely' interrupt Keras neural net training via:
early stopping callback (once accuracy improvements are small)
stopping the execution and restarting from the last saved model
However, I'm looking for a way to have a more robust way to interrupt the training.
Is there a way to cr...

1

votes

1

answer

202

Views

### Keras' ImageDataGenerator randomly throws an error when accessing image files

I am using the keras.preprocessing.image ImageDataGenerator to stream images from a folder on my hard drive. It works mostly, but the code randomly throws an error when accessing images. The error message looks like this:
img = pil_image.open(path)
File 'C:\Program Files\Anaconda3\envs\py35_cv2_nb_t...

1

votes

0

answer

52

Views

### Loss isn't decreasing in neural network

I am implementing a variant of the CNN described by this paper.
My problem is that the loss isn't decreasing and I don't understand why. Same have to be said concerning accuracy(stuck at 0.5 more or less).
This a problem of 2 classes classification. I am using the data from this website:
I suspect...

1

votes

0

answer

157

Views

### Python version or Keras?

This is my deep learning model using Keras to predict the quality of wine (wine dataset)
model = Sequential()
model.add(Dense(11, input_shape=(11,), activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='mse', opt...

1

votes

0

answer

57

Views

### Erorr using keras 2.0 in R

I am trying to replicate Siraj's code for predicting stock prices in R (https://github.com/llSourcell/How-to-Predict-Stock-Prices-Easily-Demo).
This is my code:
url

1

votes

1

answer

1.2k

Views

### Keras - ValueError: If steps_per_epoch is set, the `batch_size` must be None

No mention of such an incompatibility in the doc. What I want to do is declare a number of batches (of fixed given size) to be processed before ending an epoch and starting the next one (shuffling beforehand) in order to try to reduce overfitting. When I chose batch_size=256 and steps_per_epoch=100...

1

votes

0

answer

324

Views

### Getting very low categorical_accuracy while refitting loaded Keras model

I'm getting a very low val_categorical_accuracy on previously trained model. Steps I followed:
The model was trained for 30 epochs.
It achieved ~0.60 val_categorical_accuracy and categorical_accuracy.
It was saved using Kerases model.save() method.
On loading with Keras.models.load_model() and evalu...

1

votes

0

answer

204

Views

### Using elephas on jupyter test notebooks

I don't have a spark cluster at hand, but I want to fiddle around with elephas, so I'm using temporary instances at try.jupyter.org.
You'll find at the bottom of this question all of my code (for reproducibility), and the full error log, but here is a short description of my problem :
When I run my...

1

votes

0

answer

85

Views

### Custom Imagenet Dataset

I want to make my own custom dataset of imagenet taking 20 classes out of 1000. I am able to convert it to TFRecords format but I want a keras friendly version for it as keras is more flexible for me.

1

votes

2

answer

1.5k

Views

### Keras autoencoder negative loss and val_loss with data in range [-1 1]

I am trying to adapt keras autoencoder example to a my data. I have the following network:
Xtrain = np.reshape(Xtrain, (len(Xtrain), 28, 28, 2))
Xtest = np.reshape(Xtest, (len(Xtest), 28, 28, 2))
input_signal = Input(shape=(28, 28, 2))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_...

1

votes

0

answer

331

Views

### Why does Keras model.predict() result in different probabilities based on the size of testing data?

I'm relatively new to Keras and image classification in general and I'm running into an issue that I can't seem to find much information on.
So the gist of it is that I've written a slightly modified version of the resnet50 architecture and am testing it on my own training dataset of 5000 images. T...

1

votes

0

answer

147

Views

### Keras: Finding Close Items after Collaborative Filtering

So I have ran collaborative filtering algorithm using Keras (Tensorflow back-end, if that matters) for a games rating database.
def collaborative_filtering(num_items, num_users, num_item_features=50):
user_in = Input(shape=(1,), dtype='int64', name='user_in')
users_preferences = Embedding(
#...
name...

1

votes

1

answer

267

Views

### ValueError: Error when checking target: expected dropout_82 to have 3 dimensions, but got array with shape (7, 7500)

Hi I must implement a cnn, I'm new with Keras and Tensorflow so I'm apologizing if I'm making a mistake.
This is what I do:
the dataset is an numpy array (23, 4800000), #number of audio tracks x #number of samples.
So I splitted the dataset in train (10, 4800000), validation (7, 4800000) and test (6...

1

votes

0

answer

95

Views

### Dense layer dimension error even after using flatten

I'm new to Keras and using the following model to experiment with the MNIST problem.
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5, 5), input_shape=(28, 28, 1),
padding='same', activation='relu',
bias_initializer='RandomNormal'))
model.add(MaxPooling2D(pool_size=(2, 2), padding='...

1

votes

0

answer

383

Views

### How to Disable cuDNN or force it to have deterministic behavior when using Keras with TensorFlow backend?

I am using Keras with TensorFlow backend on GPU. How can I disable cuDNN or force it to have deterministic behavior?
There is a clear way for Theano backend as mentioned here:
https://github.com/keras-team/keras/issues/2479
But I cannot find a similar way for TensorFlow. Any help is greatly apprecia...

1

votes

0

answer

93

Views

### K.ctc_decode() memory not released

when using K.ctc_decode() like the code below in a large loop, the memory cost more and more, and time cost longer and longer.
out = K.get_value(K.ctc_decode(y_pred, input_length=np.ones(y_pred.shape[0])*y_pred.shape[1], )[0][0])[:, :]
How can I solve it ?

1

votes

1

answer

167

Views

### Keras - weights initialized as nans

I am trying to create a neural network for policy based RL. I have wrote the class to build the network and generate actions as below:
class Oracle(object):
def __init__(self, input_dim, output_dim, hidden_dims=None):
if hidden_dims is None:
hidden_dims = [32, 32]
self.input_dim = input_dim
self.out...

1

votes

1

answer

1.4k

Views

### How to implement a custom layer wit multiple outputs in Keras?

Like stated in the title, I was wondering as to how to have the custom layer returning multiple tensors: out1, out2,...outn?
I tried
keras.backend.concatenate([out1, out2], axis = 1)
But this does only work for tensors having the same length, and it has to be another solution rather than concatenat...

1

votes

0

answer

195

Views

### Keras ignoring validation_data when provided from TF Iterator

When providing validation_data from TensorFlow Iterator, Keras seems to ignore the parameter and use training data anyway. Is my approach incorrect, or is it a bug in Keras?
import tensorflow as tf
import keras
def _parse_function_x(filename):
image = tf.random_uniform([tf.shape(filename)[0], 198,19...

1

votes

1

answer

722

Views

### Choosing only specific classes of CIFAR-10

I'd like to use CIFAR-10 dataset but I want only the frog,dog,cat,horse and bird classes, I've used the following code so far :
# Plot ad hoc CIFAR10 instances
from keras.datasets import cifar10
from matplotlib import pyplot
from scipy.misc import toimage
# load data
(X_train, y_train), (X_test, y_...

1

votes

0

answer

254

Views

### Exporting/Importing Keras Model to Tensorflow fails when using multi_gpu_model

I'm currently struggeling with importing my exported Keras model into Tensorflow. The code worked fine with a sequential model. I was able to train the model in python and then import it into my c++ application. Since I needed more ressources I decided to distribute the model onto several GPUs. Afte...

1

votes

0

answer

261

Views

### How to use two loss functions for a single model in keras?

I want to use one keras model for two different purposes. One using a CTC Loss Function and Another using Categorical Crossentropy loss function.
This is my model in keras:
def train(run_name, start_epoch, stop_epoch, img_w,type_t):
input_data = Input(name='the_input', shape=input_shape, dtype='flo...

1

votes

0

answer

1.1k

Views

### installed keras and tensorflow but getting error

i am getting the this importerror
even i have installed all the necessary modules like keras , tensorflow etc,
i am not able to resolve it even working in conda environment.
ImportError: No module named python.training
#Imports
import os
from random import shuffle
#Keras imports
from keras.preproc...

1

votes

0

answer

315

Views

### Attention mechanism in spelling correction model

I'm trying to test attention mechanism in this code (based on the work of MajorTal):
def generate_model(output_len, chars=None):
'''Generate the model'''
print('Build model...')
chars = chars or CHARS
model = Sequential()
# 'Encode' the input sequence using an RNN, producing an output of hidden_size...

1

votes

0

answer

32

Views

### Keras RNN - ValueError when checking Input

I recently got a Nvidia Card and wanted to try LSTM-Models with the new GPU-Support. Sadly I do not know much about LSTMs. And I build this little model to test it:
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
from sklearn.model_selection impo...

1

votes

0

answer

389

Views

### Uncomprehensibly high loss value in regression

Based on some research and experimentation, I'm trying to build a Keras regressor for a few select input attributes which I've run feature selection on to determine importance. Now, the loss is insanely high, along the lines of loss: 70155460.5246 - mean_squared_error: 70155460.5246. Scaling the in...

1

votes

0

answer

116

Views

### Solving FizzBuzz with Keras

I am trying to solve FizzBuzz using Keras and it works quite well for numbers between 1 and 10.000 (90-100% win rate and close to 0 loss). However, if I try even higher numbers, that is numbers between 1 and 100.000 it doesn't seem to perform well (~50% win rate, loss ~0.3). In fact, it performs qui...

1

votes

1

answer

716

Views

### model.fit() generates ERROR: ValueError: All input arrays (x) should have the same number of samples

I have a keras model with several custom layers. When I run:
model_.compile(optimizer=rms, loss=contrastive_loss,metrics=['accuracy'])
It compiles without any problems.
But when I try to fit the model with a list of arrays:
X = [T1,R1] + [T2, R2]
model_.fit(X, [None]*2, epochs=50, batch_size=32)
I...

1

votes

2

answer

1.1k

Views

### Keras Dense Net Overfitting

I am attempting to use keras to build an activity classifier from accelerometer signals. However, I am experiencing extreme overfitting of the data even with the most simplistic of models.
The input data is of shape (10,3) and contains roughly .1 second of data from the accelerometer in 3 dimension...

1

votes

0

answer

23

Views

### How to design an RNN to treat all training set as a time series in Keras?

I am working on a classification problem, where we have a pairwise input,
X1 = Input(shape=(input_size,), name='input_1')
X2 = Input(shape=(input_size,), name='input_2')
and the expected output is binary.
To solve this problem, I first design an encoder which takes these pairwise inputs and encode t...

1

votes

2

answer

170

Views

### Error Input shape is not the one expected

I've seen a lot of posts about this kind of input shape error with Keras but I still don't get it.
I am trying to resolve a classification problem with Keras. My x_data (for 1 example!) is an array of arrays (4 arrays of length 40). The number of classes to predict at the end is 5.
There is a mismat...

1

votes

1

answer

147

Views

### Error while adding Conv1D layer

I am trying to train on a data containing sequences of 43 records of 3-dimensional vectors. While trying to add this Conv1D layer here:
model = Sequential()
model.add(Conv1D(input_shape=(43, 3),
filters=16,
kernel_size=4,
padding='same')) # This is line 24 of bcl_model_builder.py
model.add(BatchNo...

1

votes

0

answer

103

Views

### How to create a tensor from a float value?

I'm using keras to build NN.
I need to make custom evaluation function (which I did but not completely).
Here is part of the code:
from keras import backend as K
...
def customized_loss(y_true, y_pred):
loss = K.square(y_true - y_pred)
return K.sum(loss)
...
model.compile(loss=customized_loss, opt...

1

votes

0

answer

247

Views

### Keras- Binarizing Weights Prior to Forward and Backwards Propogation

I am trying to replicate the binarization process used in https://github.com/MatthieuCourbariaux/BinaryConnect on a DNN constructed in Keras, where prior to determining the weight gradients they are binarized stochasticly using the following two functions:
# hard_sigmoid(); Clips the input, x, to [0...

1

votes

1

answer

53

Views

### What should be the input to Convolution neural network (CNN) using keras and tensorflow?

I'm trying to create CNN model using keras ad tensorflow as backend.
below is code for the same..
Cannot understand what input it is expecting...
import cv2,os
import glob
import numpy as np
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras.utils impo...

1

votes

0

answer

131

Views

### a TypeError in fit function when using keras with theano backend

my theano version is 1.0.1+unknown.
my keras version is 2.1.3
i'm implementing stacked LSTM autoencoder in keras and this is my code:
timesteps = x_train.shape[1]
input_dim = 1
inputs = Input(batch_shape=(1,timesteps, input_dim))
encoded = LSTM(7,return_sequences = True)(inputs)
encoded = LSTM(5,ret...

1

votes

0

answer

202

Views

### model save in keras json file

I saw the two different kinds of model saving style in Keras.
model.save(os.path.join(model_path, Filename))
and other one uses json and weight
model_json = model1.to_json()
with open('model1.json', 'w') as json_file:
json_file.write(model_json)
model1.save_weights('model1.h5')
print('Saved model t...

1

votes

0

answer

531

Views

### Keras LSTM model has very low accuracy

I am trying to build a model that takes a sentence as input, takes each word and tries to predict the next word. My input and output both are a 3D matrix with (number of sentences, number of words per sentence, dimension of word embedding). Input is e. g. 'I like green apples' while the output is 'l...

1

votes

1

answer

64

Views

### How is Theano based ElemwiseSumLayer of Lasagne converted in Keras

I have a Net built in Theano. I am trying to convert it to Keras.
net['voxres2_out'] = ElemwiseSumLayer([net['conv1c'], net['voxres2_conv2']])
Now, I want a conversion of this statement in Theano to Keras. However, I am unsure about which layer would be a substitute of the same functionality in Kera...