4
$\begingroup$

I'm new in using convolutional neural networks with keras. I can train a CNN for classify somethings and in other words for discrete output, but I can't find an example for getting continuous output (linear regression,...) in keras. Could you give me a link for this? or explain it yourself?

Thanks in advance.

here is my code:

 def MakeConvNet(WindowSize):
batch_size = 128
nb_classes = 2
nb_epoch = 10

# input image dimensions
img_rows, img_cols = WindowSize, WindowSize
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 4
# convolution kernel size
kernel_size = (4, 4)

# the data, shuffled and split between train and test sets
x_dim_In,y_dim_In,z_dim_In=Input.shape
x_dim_Out,y_dim_Out=Output.shape
temp1=int(math.floor(40*x_dim_In/100));
temp2=int(math.floor(50*x_dim_In/100)); 
temp3=int(math.floor(90*x_dim_In/100));
temp4=int(math.floor(40*x_dim_Out/100));
temp5=int(math.floor(50*x_dim_Out/100));    
temp6=int(math.floor(90*x_dim_Out/100));

X_train_1=Input[0:temp1,:]
X_train_2=Input[temp2:temp3,:]
X_train=np.concatenate((X_train_1,X_train_2),axis=0)
X_test_1=Input[temp1:temp2,:]
X_test_2=Input[temp3:x_dim_In]
X_test=np.concatenate((X_test_1,X_test_2),axis=0)
y_train_1=Output[0:temp4,:]
y_train_2=Output[temp5:temp6,:]
y_train=np.concatenate((y_train_1,y_train_2),axis=0)
y_test_1=Output[temp4:temp5,:]
y_test_2=Output[temp6:x_dim_Out,:]
y_test=np.concatenate((y_test_1,y_test_2),axis=0)
X_train = X_train.reshape(X_train.shape[0],1,img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0],1,img_rows, img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices

y_train=y_train.astype(int)
y_test=y_test.astype(int)
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()

model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
                    border_mode='valid',
                    input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
          optimizer='adadelta',
          metrics=['accuracy'])


checkpointer = ModelCheckpoint(filepath="./weights_Eye.hdf5", verbose=1, save_best_only=True)
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, Y_test), callbacks=[checkpointer])
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

model_json = model.to_json()
with open("model_Eye_30.json", "w") as json_file:
    json_file.write(model_json)

# serialize weights to HDF5
model.save_weights("model_Eye_30.h5")
print("Saved model to disk")
$\endgroup$

closed as off-topic by Tim Nov 9 '17 at 13:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Tim
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ The output is continuous by default. In your classification CNN you must have added a sigmoid or softmax to the output layer. Can you post your code for building your classification Keras model and we can point out the lines you need to remove $\endgroup$ – Hugh Nov 1 '16 at 14:31
  • $\begingroup$ Thanks for your reply, for example see this link: ml4a.github.io/guides/convolutional_neural_networks . In this example we convert type of y to categorical before everything! and also we have n_classes. $\endgroup$ – Panda Nov 1 '16 at 14:48
  • $\begingroup$ and also in this example : pyimagesearch.com/2016/08/01/… $\endgroup$ – Panda Nov 1 '16 at 14:51
  • $\begingroup$ Any chance of getting that link back? I am getting a 404 error. EDIT: found it: github.com/ml4a/ml4a-guides/blob/master/notebooks/… $\endgroup$ – ashley Aug 4 '17 at 16:55
2
$\begingroup$

Taking the model from the first link you gave in the comments; this is the code use after convolutional layers are applied (I've removed the parts relating to dropout because that's irrelevant to your question).

# flatten the data for the 1D layers model.add(Flatten())
# Dense(n_outputs) 
model.add(Dense(128)) 
model.add(Activation('relu'))

# the softmax output layer gives us a probablity for each class 
model.add(Dense(n_classes))
model.add(Activation('softmax'))

The line "model.add(Dense(n_classes))" adds an output layer of size n_classes. At this point the output is continuous, it's the sum of all outputs from the previous layer multiplied by the weights.

The tutorial then adds a softmax activation function which puts all the outputs into the range [0,1]. You just need to remove this "model.add(Activation('softmax'))" to get a continuous output.

$\endgroup$
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – whuber Nov 4 '16 at 15:14
  • $\begingroup$ the link is dead $\endgroup$ – Stepan Yakovenko Jun 19 at 9:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.