1
$\begingroup$

I am new to Tensorflow and machine learning.

I am trying to use high level API from Tensorflow.

Please tell me what i am doing wrong.

import tensorflow as tf
import numpy as np


features = np.array([[-1,-2],[1,2]],dtype='int32')
label = np.array([0,1],dtype='int32')

feature_columns = [tf.feature_column.numeric_column('features',shape=[2,2])]

model = tf.estimator.LinearClassifier(feature_columns=feature_columns,n_classes=2)

model.train(input_fn= tf.estimator.inputs.numpy_input_fn(x=features,y=label,shuffle=True))

I am getting an error

ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.ops.Tensor'>
$\endgroup$
  • $\begingroup$ The error is for which line? Also please say the version of your tensorflow. $\endgroup$ – Media Jun 11 '18 at 12:13
  • $\begingroup$ Also take a look at here. $\endgroup$ – Media Jun 11 '18 at 12:19
  • $\begingroup$ Hi @Media, Thank you so much for your comments. I get errror when i try to train the model model.train(input_fn= tf.estimator.inputs.numpy_input_fn(x=features,y=label,shuffle=True)) Versions: tensorflow 1.8 python 3.5.2 $\endgroup$ – maswadkar Jun 11 '18 at 14:04
  • $\begingroup$ I guess the provided link has the answer, did you figured it out? $\endgroup$ – Media Jun 11 '18 at 14:27
0
$\begingroup$

x, y that goes in tf.estimator.inputs.numpy_input_fn() must be either an array or a dictionary of arrays. Quoting from the link (https://www.tensorflow.org/api_docs/python/tf/estimator/inputs/numpy_input_fn) - x: numpy array object or dict of numpy array objects. If an array, the array will be treated as a single feature.

So in your case it is treating 'feature' as a single feature. You can try this:

x1 = np.array([-1,-2])

x2 = np.array([1,2])

features = {'x1': x1, 'x2': x2}
| improve this answer | |
$\endgroup$
0
$\begingroup$

I found answer to my problem. I must make a change in one line and everything works

change is

model.train(input_fn= tf.estimator.inputs.numpy_input_fn(x={'features' : features}, 
                                                         y=label,
                                                         shuffle=True))
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.