2
votes

I'm trying to build a Deep Neural Network classifier modeled after the example in the TensorFlow directory. The code of the example is shown here:

def main(unused_argv):
  # Load dataset.
  iris = learn.datasets.load_dataset('iris')
  x_train, x_test, y_train, y_test =     cross_validation.train_test_split(
      iris.data, iris.target, test_size=0.2, random_state=42)

  # Build 3 layer DNN with 10, 20, 10 units respectively.
  classifier = learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3)

  # Fit and predict.
  classifier.fit(x_train, y_train, steps=200)
  score = metrics.accuracy_score(y_test, classifier.predict(x_test))
  print('Accuracy: {0:f}'.format(score)) 

I'm doing the exact same thing except I'm using my data, which is the same as the iris data (continuous feature values and discrete 0 or 1 target values). A sample of my data is shown here:

  G1           G2          G3         G4        Target
7.733347    6.933914    6.493334    5.31336         0
6.555225    6.924448    6.353376    5.568334        1
7.515558    6.326627    6.197123    5.565245        0
7.132243    6.733111    7.107221    5.681575        1

I'm reading my data with the following code:

def extract_examples_labels(filepath):
    data = pd.read_csv(filepath).as_matrix()
    num_inputs = len(data[0])-1
    data_examples = data[:,range(num_inputs)]
    data_labels= data[:,len(data[0])-1]
    return data_examples, data_labels

I then do the EXACT same thing as in the TensorFlow example but I use my data instead. However, I keep getting an error that says:

ValueError: Target's dtype should be int32, int64 or compatible. Instead got dtype: 'float64'

So I figure this means that since my y_train is a float, I need to cast it to an int so I do so using:

y_train = y_train.astype(int)

I confirm its of type int64 and run the classifier again but get the following error:

ValueError: Targets are incompatible with given information. Given targets: Tensor("output:0", shape=(?,), dtype=int64), required signatures: TensorSignature(dtype=tf.float64, shape=TensorShape([Dimension(None)]), is_sparse=False).

Now it says it wants a float64. So I'm confused what I'm doing wrong. Any suggestions or obvious mistakes?

1
Your Target column is being interpreted as float type. You can do data[['Target']] = data[['Target']].astype(int) to convert them as int type.Nickil Maveli
I've already tried that. As you can see, y_train is my target and I cast it to an int type but I get a different error which I don't understand.g00glechr0me

1 Answers

2
votes

After doing a little digging, I found a solution. If you look in the following directory in the TensorFlow package:

tensorflow.contrib.learn.python.learn.datasets

You can find a file called base.py which has the csv file loading functions. Basically, I just modified the function called load_csv to take in my file. The code is shown below:

Dataset = collections.namedtuple('Dataset', ['data', 'target'])
Datasets = collections.namedtuple('Datasets', ['train', 'validation', 'test'])

def load_csv(filename, target_dtype, target_column=-1,    has_header=True):
   """Load dataset from CSV file."""
  with gfile.Open(filename) as csv_file:
    data_file = csv.reader(csv_file)
    if has_header:
       header = next(data_file)
       n_samples = int(header[0])
       n_features = int(header[1])
       data = np.empty((n_samples, n_features))
       target = np.empty((n_samples,), dtype=np.int)
       for i, ir in enumerate(data_file):
            target[i] = np.asarray(ir.pop(target_column), dtype=target_dtype)
            data[i] = np.asarray(ir, dtype=np.float64)
    else:
       data, target = [], []
       for ir in data_file:
       target.append(ir.pop(target_column))
       data.append(ir)
  return Dataset(data=data, target=target)

So if you see the code above, I think the problem I was having is the target_dtype attribute. Even though I changed the dtype of the target array, I didn't change the target_dtype attribute, which made it seem incompatible when TensorFlow checked the signatures. My code works now =. If you have any questions or can clarify this further please feel free to do so!