koifish
koifish

Reputation: 444

Tensorflow: Logits and labels must have the same first dimension

I'm new to machine learning in TF. I have this dataset which I generated and exported into a .csv file. It is here: tftest.csv.

The 'distributions' column corresponds to a unique system of equations which I have tried to condense down into a series of digits in SageMath. The 'probs' column correspond to whether one should mutiply a given equation by a given monomial of the equation, based on the row and column it is located in. The above is just for overview and is not related to my actual question.

Anyways, here's my code. I've tried to explain it as best as I can with annotations.

import csv
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.keras as keras

distribution_train = []
probs_train = []
# x_train = []
# y_train = []

with open('tftest.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')

    for row in csv_reader:
        distribution_train.append(row[0])
        probs_train.append(row[1])

'''
Get rid of the titles in the csv file
'''
distribution_train.pop(0)
probs_train.pop(0)

'''
For some reason everything in my csv file is stored as strings.
The below function is to convert it into floats so that TF can work with it.
'''
def num_converter_flatten(csv_list):
    f = []
    for j in range(len(csv_list)):
        append_this = []
        for i in csv_list[j]:
            if i == '1' or i == '2' or i == '3' or i == '4' or i == '5' or i == '6' or i == '7' or i == '8' or i =='9' or i =='0':
                append_this.append(float(i))
        f.append((append_this))

    return f

x_train = num_converter_flatten(distribution_train)
y_train = num_converter_flatten(probs_train)

x_train = tf.keras.utils.normalize(x_train, axis=1)
y_train = tf.keras.utils.normalize(y_train, axis=1)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))

'''
I'm making the final layer 80 because I want TF to output the size of the
'probs' list in the csv file
'''

model.add(tf.keras.layers.Dense(80, activation=tf.nn.softmax))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)

However, when I run my code, I get the following error.

tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [32,80] and labels shape [2560]
 [[{{node loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}} = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT64, _class=["loc:@train...s_grad/mul"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](loss/output_1_loss/Log, loss/output_1_loss/Cast)]]

I searched online for this error, but I can't seem to understand why it's cropping up. Can anyone help me understand what's wrong with my code? If there are any questions as well, please leave a comment and I'll do my best to answer them.

Upvotes: 0

Views: 6246

Answers (1)

jhso
jhso

Reputation: 3283

I managed to get your code working with some changes, it appears the error was occuring due to your use of "sparse_categorical_crossentropy." I don't know why you were using this as your classes don't seem to be exclusive, ie. your have a score of '1' at several rows in your tftest.csv for each entry. Also, you shouldn't normalize your labels. I made these changes:

x_train = num_converter_flatten(distribution_train)
y_train = num_converter_flatten(probs_train)

x_train = tf.keras.utils.normalize(x_train, axis=1)
y_train = np.array(y_train)#tf.keras.utils.normalize(y_train, axis=1)

and further down:

model.add(tf.keras.layers.Dense(80, activation=tf.nn.sigmoid))

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

Again, since your classes don't seem to be exclusive, you shouldn't use the softmax activation.

But since the code now works, you can work on optimizing things (it didn't seem to train well for the 5 epochs I ran it for).

Upvotes: 1

Related Questions