# How to get continuous output with Convolutional network? (Keras) [closed]

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?

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()

border_mode='valid',
input_shape=(1, img_rows, img_cols)))

model.compile(loss='categorical_crossentropy',
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")

• 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
– Hugh
Nov 1 '16 at 14:31
• 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. Nov 1 '16 at 14:48
• and also in this example : pyimagesearch.com/2016/08/01/… Nov 1 '16 at 14:51
• Any chance of getting that link back? I am getting a 404 error. EDIT: found it: github.com/ml4a/ml4a-guides/blob/master/notebooks/… Aug 4 '17 at 16:55

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)

# the softmax output layer gives us a probablity for each class