在python3.5.2中使用TensorFlow版本1.3.0。我试图在TensorFlow网站上的Iris数据教程中模拟dnnclassier的功能,但遇到了一些困难。我导入了一个包含155行数据和15列数据的CSV文件,将数据分解为训练和测试数据(在这里我尝试对正迁移或负迁移进行分类),当我开始训练分类器时,收到一个错误。下面是如何设置数据的
#import values from csv
mexicof1 = pd.read_csv('Source/mexicoR.csv')
#construct pandas dataframe
mexico_df = pd.DataFrame(mexicof1)
#start counting from mexico.mat.2.nrow.mexico.mat...1.
mexico_dff = pd.DataFrame(mexico_df.iloc[:,1:16])
mexico_dff.columns = ['tp1_delta','PC1','PC2','PC3','PC4','PC5','PC6','PC7', \
'PC8', 'PC9', 'PC10', 'PC11', 'PC12', 'PC13', 'PC14']
#binary assignment for positive/negative values
for i in range(0,155):
if(mexico_dff.iloc[i,0] > 0):
mexico_dff.iloc[i,0] = "pos"
else:
mexico_dff.iloc[i,0] = "neg"
#up movement vs. down movement classification set up
up = np.asarray([1,0])
down = np.asarray([0,1])
mexico_dff['tp1_delta'] = mexico_dff['tp1_delta'].map({"pos": up, "neg": down})
#Break into training and test data
#data: independent values
#labels: classification
mexico_train_DNN1data = mexico_dff.iloc[0:150, 1:15]
mexico_train_DNN1labels = mexico_dff.iloc[0:150, 0]
mexico_test_DNN1data = mexico_dff.iloc[150:156, 1:15]
mexico_test_DNN1labels = mexico_dff.iloc[150:156, 0]
#Construct numpy arrays for test data
temptrain = []
for i in range(0, len(mexico_train_DNN1labels)):
temptrain.append(mexico_train_DNN1labels.iloc[i])
temptrainFIN = np.array(temptrain, dtype = np.float32)
temptest = []
for i in range(0, len(mexico_test_DNN1labels)):
temptest.append(mexico_test_DNN1labels.iloc[i])
temptestFIN = np.array(temptest, dtype = np.float32)
#set up NumPy arrays
mTrainDat = np.array(mexico_train_DNN1data, dtype = np.float32)
mTrainLab = temptrainFIN
mTestDat = np.array(mexico_test_DNN1data, dtype = np.float32)
mTestLab = temptestFIN
这样做得到的数据如下所示:
^{pr2}$在遵循这个给定设置的教程之后,我可以运行代码到分级机.列车()函数停止运行并给出以下错误:
# Specify that all features have real-value data
feature_columns = [tf.feature_column.numeric_column("x", shape=[mexico_train_DNN1data.shape[1]])]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
optimizer = tf.train.AdamOptimizer(0.01),
n_classes=2) #representing either an up or down movement
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x = {"x": mTrainDat},
y = mTrainLab,
num_epochs = None,
shuffle = True)
#Now, we train the model
classifier.train(input_fn=train_input_fn, steps = 2000)
File "Source\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\estimator\canned\head.py", line 174, in _check_labels
(static_shape,))
ValueError: labels shape must be [batch_size, labels_dimension], got (128, 2).
我不知道为什么我会遇到这个错误,任何帮助都是感激的。在
当
DNNClassifier
需要一个类标签(即0或1)时,您使用的是一个热([1,0]或[0,1])编码的标签。解码最后一个轴上的一个热编码,使用注意,对于二进制文件来说,这样做可能更快
^{pr2}$虽然性能差异不会很大,我可能会使用更通用的版本,以防以后添加其他类。在
在您的例子中,在生成numpy标签之后,只需添加
相关问题 更多 >
编程相关推荐