0

I am trying to build a Tensorflow simple AI and I don't understand where my problem is when I compare it to various articles I read about this topic.

I have two numpy arrays made for training of sizes (N,1) (what should be predicted) and (N,3) (what are the inputs that should be predicted), and I want my algorithm to predict with Input[0,i] Input[1,i] Input[2,i] the value of ToPredict[i] (the i-th sample). I thought I had to use tf.data.Dataset.from_tensor_slices and then

modele = tf.keras.Sequential([ tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(1) ]) 
modele.compile(optimizer=tf.keras.optimizers.RMSprop(), loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['sparse_categorical_accuracy'])
modele.fit(A, epochs=10)
from an example I got online. (here A represents the tf.data.Dataset.from_tensor_slices(Inputs_Learning,ToPredict_Learning))

When I do that, I get the following error:

logits and labels must have the same first dimension, got logits shape [3,10] and labels shape [1] [[node loss_9/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at :11) ]] [Op:__inference_keras_scratch_graph_2640]

So I know that there is a shape error, but I don't know how to tell the algorithm what I want as shapes.

Could anyone help me? Also yes I have seen related stackoverflow posts but they don't seem to solve my issue.

1 Answer 1

1

Add batch dimension to your data:

A = A.batch(10)
Sign up to request clarification or add additional context in comments.

7 Comments

I changed the number in Dense. However when I transpose my data, Input becomes (3,N) and ToPredict (1,N) numpy arrays. Then, when I do tf.data.Dataset.from_tensor_slices( it returns the following error: ValueError: Dimensions 3 and 1 are not compatible @Andrey
transpose labels as well
The previous code was tf.data.Dataset.from_tensor_slices(A,B) and I changed it to tf.data.Dataset.from_tensor_slices(A.T,B.T). So I assume what you call "Labels" is B (the value to predict), which I did transpose in my previous comment
input to the network should be (N, 3), labels (what is predicted) should be (N, 1)
Ok so I still get the error mentionned in the question. The confusion arises from the fact that I said (1,N) instead of (N,1) multiple times including in the question. Sorry. Now I have two Dataset(A,B) with A and B of shapes (N,3) and (N,1) as you said and it gives the error in the question in the "fit" line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.