对于Tensorflow的开始,我正在试图用IRIS数据集重现估算器的基本示例,但是我自己的数据。
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from six.moves.urllib.request import urlopen
import tensorflow as tf
from pandas import DataFrame, read_csv
import numpy as np
TESTFILE = "test.csv"
TRAINFILE = "train.csv"
# Load datasets
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename=TRAINFILE, target_dtype=np.int, features_dtype=np.float32, target_column=0)
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename=TESTFILE, target_dtype=np.int, features_dtype=np.float32, target_column=0)
# Specify that all features have real-value data
feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/Goldman_model")
# Define the training inputs
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)
# Train model.
classifier.train(input_fn=train_input_fn, steps=2000)
但是,当我尝试训练分类器时,我收到下一个错误:
File "C:\Users\***\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 691, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 160 and 128
for 'dnn/head/sparse_softmax_cross_entropy_loss/xentropy/xentropy'
(op: 'SparseSoftmaxCrossEntropyWithLogits') with input shapes: [160,3], [128].
我完全不知道下一步该做什么。
非常感谢您的回答,
JF Palomeque
答案 0 :(得分:1)
您的标签和预测有不同的尺寸(160和128)。