TransWikia.com

error when input function called with shape (2,2)

Data Science Asked by maswadkar on August 22, 2020

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'>

2 Answers

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}

Answered by naive on August 22, 2020

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))

Answered by maswadkar on August 22, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP