Reputation: 438
I'm trying to make a neural network in Keras, but in doing so have come across a problem: the output size of the network isn't the same as the output I'd like, which is a single number. I have tried doing this by adding a Dense layer with the "units" argument set to 1. However, due to the extra dimensions of my input data, I still end up with an output of shape (None, 13, 8, 1).
import numpy
from keras.layers import Conv2D, Input, Dense
from keras.models import Model
from keras.optimizers import SGD
def make_training_data():
input_training_data = []
for i in range(6):
a = []
for j in range(13):
b = []
for k in range(8):
c = []
for l in range(8):
c.append(0)
b.append(c)
a.append(b)
input_training_data.append(a)
output_training_data = []
for i in range(6):
output_training_data.append(0)
return input_training_data, output_training_data
def make_neural_net():
input = Input((13, 8, 8))
output = Conv2D(256, (3, 3), input_shape=(13, 8, 8), padding='same')(input)
output = Dense(1)(output)
return Model(outputs=output, inputs=input)
def main():
input_training_data, output_training_data = make_training_data()
neural_net = make_neural_net()
input_training_data = numpy.array(input_training_data)
output_training_data = numpy.array(output_training_data)
sgd = SGD(0.2, 0.9)
neural_net.compile(sgd, 'categorical_crossentropy', metrics=['accuracy'])
neural_net.fit(input_training_data, output_training_data, epochs=1)
main()
How can I change my network so that I get the right size of output?
Upvotes: 1
Views: 421
Reputation: 36584
You need to flatten your tensors before you turn a convolutional layer into a 1D tensor.
Replace:
output = Dense(1)(output)
with:
output = Flatten()(output)
output = Dense(1)(output)`
Import it from keras.layers.Flatten
Also, since you have 1 output neuron, you will need to change your activation function to 'sparse_categorical_crossentropy'
Upvotes: 3