Reuben Walker
Reuben Walker

Reputation: 9

Keras shapes input for training and prediction raising value and attribute errors

I am having difficulty inputting data into a simple keras model for both predicting and training. I have looked at the related questions but after spending 6 hours attempting to figure this out on my own I am still struggling with this.

Imports and network:

from tensorflow import keras
from tensorflow.keras.layers import Dense
from tensorflow import convert_to_tensor
from random import randint

model = keras.Sequential()
model.add(Dense(3, activation='relu', input_dim=5))
model.add(Dense(4, activation='sigmoid'))
model.add(Dense(2))
model.compile(loss=keras.losses.MeanSquaredError(),
              optimizer='adam',
              metrics=['MeanSquaredError'])

Attempting to predict with the network:

X = [4, 5, 2, 8, 9]
tensor = convert_to_tensor(X)
print(tensor.shape) # Prints (5, ) as expected
prediction = model.predict(tensor)

The following error is raised:

ValueError: Error when checking input: expected dense_input to have shape (5,) but got array with shape (1,)

Trying to train the network:

states = [[7, 8, 3, 9, 7], [0, 7, 5, 0, 3], [2, 5, 8, 1, 10], [9, 9, 9, 8, 6]]
actions = [[1, 0], [0, 1], [0, 1], [0, 1]]

tensor_states = convert_to_tensor(states)
tensor_actions = convert_to_tensor(actions)

print(tensor_states.shape)      # Prints (4, 5)
print(tensor_actions.shape)     # Prints (4, 2)

model.fit(tensor_states, tensor_actions, verbose=0)

The following error is raised:

ValueError: TypeError: len() of unsized object

Without verbose=0 I get this error:

AttributeError: 'ProgbarLogger' object has no attribute 'log_values'

I am running in a conda environment.

I asked a previous question on this topic but I realized I overcomplicated the issue and was not exactly sure what I was asking. Now that I have done more research I feel I have boiled down my issue to the most basic form. I wrote this new question after consideration of this meta post. If I should have just edited my previous question please let me know.

EDIT - Trouble Shooting with darcycp

As the answer given below points out I should be giving a column to predict the network (using two sets of brackets). However, this gives the following error:

ValueError: TypeError: len() of unsized object

Then not using the tensor (as keras predict function can directly accept python lists) gives the following error:

ValueError: Attempt to convert a value (4) with an unsupported type () to a Tensor

Which is extremely unusual to me as I do not have numpy imported into this program.

Upvotes: 0

Views: 109

Answers (1)

darcycp
darcycp

Reputation: 131

What I think is going on here is that your tensor X is a tensor of 5, 1-D inputs. When what you want it to be is a single tensor with one input, where a valid input for this model is made up of 5 numbers.

Try declaring instead as:

X =  [[4, 5, 2, 8, 9]]

You can think of X as a array that can be full of different instances of inputs to your task. Each one of those instances is itself an array of inputs. While in this case X only has 1 input in another siutation it may look like:

X =  [[4, 5, 2, 8, 9],[6,7,9,2,5]]

That is to say X is now a list of len() == 2, each item in that list is a input to your model. Keras layers by default assume that X will consist of batches of input data.

Upvotes: 1

Related Questions