0

I also encounter the same problem Training TensorFlow for Predicting a Column in a csv file which is:

AttributeError Traceback (most recent call last) in () 1 for i in range(1000): ----> 2 batch_xs, batch_ys = data.train.next_batch(100) 3 sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

AttributeError: 'numpy.ndarray' object has no attribute 'train'

How do you able to solve it?

from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

# Import MNIST data
#from tensorflow.examples.tutorials.mnist import input_data
#mnistt = input_data.read_data_sets("/tttmp/data/", one_hot=True)

from numpy import genfromtxt

import csv
import tensorflow as tf
%matplotlib inline

# Read data...
x_input = genfromtxt('Data_Coffee.csv',delimiter=',')
y_input = genfromtxt('Class_Coffee.csv',delimiter=',')

data=genfromtxt('Data_Coffee.csv',delimiter=',')

matSize = np.shape(data)

# Parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100
display_step = 1


# tf Graph input
x = tf.placeholder(tf.float32, [None, matSize[0]])
y = tf.placeholder(tf.float32, [None, matSize[1]])

#x= genfromtxt('Data_Coffee.csv',delimiter=',')
#y= genfromtxt('Class_Coffee.csv',delimiter=',')


# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(x.train.num_examples/batch_size)

        # Loop over all batches
        for i in range(total_batch):
            batch_x, batch_y = data.train.next_batch(batch_size)
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
            # Compute average loss
            avg_cost += c / total_batch
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "cost=", \
                "{:.9f}".format(avg_cost))
    print("Optimization Finished!")
6
  • here x_input is just a numpy array. you need to write ur own train data iterator Commented Aug 21, 2017 at 7:17
  • Thank you for your reply. Could you elaborate more? Any example and link to follow up? I think i need to feed data to tensorflow, but I am not sure how to do it Commented Aug 21, 2017 at 7:20
  • error log quoted Commented Aug 21, 2017 at 7:26
  • The error log is: AttributeError: 'numpy.ndarray' object has no attribute 'train' Commented Aug 21, 2017 at 7:30
  • Isn't the creation of a tensorflow object (one with train method) from a numpy array (or arrays) a basic part of a tensorflow documentation or tutorial? If you've gotten this far in setting up the problem, surely you've encountered instructions on how to create such an object. Commented Aug 21, 2017 at 16:11

1 Answer 1

1

I think you are copying this pattern from MNIST example: data.train.next_batch

In MNIST example the data is read as an object of a class that has train variable, whereas you are only reading the data as a NumPy array.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I am. the original is from MNIST example: mnist.train.next_batch. How can I edit for my data?

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.