Benny Müller
Benny Müller

Reputation: 175

Tensorflow, ValueError: setting an array element with a sequence

When trying to train my tensorflow graph im getting the error message:

ValueError: setting an array element with a sequence

happening in this line of code, in the feed_dict function:

# run the session and train the model
        _, c = sess.run([optimizer, cost], feed_dict = {input_x: x_train_v, output_y: y_train})

It seems to be a problem with my output variable (y_train). It is a List of size(25) inside a pandas dataframe. Already checked if every list has the same length with

print(y_train.shape) #(23904,)
print(y_train.apply(type)[0]) #<class 'list'>

n = len(y_train[0])
if all(len(x) == n for x in y_train):
    print("true")  #true  

The variable is created with following code:

dataframe['category_number'] = ""
for _ in range(len(dataframe)):
    string = dataframe.at[_, 'Product Categorization Tier 1'].strip()
    number = category_list.index(string)   
    # saving as category vector
    vector = [0] * 25 
    vector[number] = 1
    dataframe.at[_,'category_number'] = vector

y_train = train_df["category_number"]

Edit: Cost Function and Optimizer

prediction = neural_network_model(input_x )
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=output_y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

Full Error Message: enter image description here

Upvotes: 2

Views: 238

Answers (1)

Jonathan R
Jonathan R

Reputation: 3928

Try something like

y_train = []
for _ in range(len(dataframe)):
    string = dataframe.at[_, 'Product Categorization Tier 1'].strip()
    number = category_list.index(string)   
    # saving as category vector
    vector = [0] * 25 
    vector[number] = 1
    y_train.append(vector)

And make sure you get a 2d-int array and not an array of objects

Upvotes: 0

Related Questions