我有以下形式的数据:
A B C D E F G
1 0 0 1 0 0 1
1 0 0 1 0 0 1
1 0 0 1 0 1 0
1 0 1 0 1 0 0
...
1 0 1 0 1 0 0
0 1 1 0 0 0 1
0 1 1 0 0 0 1
0 1 0 1 1 0 0
0 1 0 1 1 0 0
A,B,C,D
是我的输入,E,F,G
是我的输出。我使用 TensorFlow 在 Python 中编写了以下代码:
from __future__ import print_function
#from random import randint
import numpy as np
import tflearn
import pandas as pd
data,labels =tflearn.data_utils.load_csv('dummy_data.csv',target_column=-1,categorical_labels=False, n_classes=None)
print(data)
# Build neural network
net = tflearn.input_data(shape=[None, 4])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 3, activation='softmax')
net = tflearn.regression(net)
# Define model
model = tflearn.DNN(net)
#Start training (apply gradient descent algorithm)
data_to_array = np.asarray(data)
print(data_to_array.shape)
#data_to_array= data_to_array.reshape(6,9)
print(data_to_array.shape)
model.fit(data_to_array, labels, n_epoch=10, batch_size=3, show_metric=True)
我收到一条错误消息:
ValueError: Cannot feed value of shape
(3, 6)
for Tensor'InputData/X:0'
, which has shape'(?, 4)'
我猜测这是因为我的输入数据有 7 列 (0...6),但我希望输入层仅将前四列作为输入,并预测数据中的最后 3 列作为输出。我该如何建模?
最佳答案
如果数据采用 numpy 格式,则前 4 列将通过简单的切片获取:
data[:,0:4]
:
表示“所有行”,0:4
是一系列值 0,1,2,3
,前 4 列。
如果数据不是 numpy 格式,只需将其转换为 numpy 格式,以便轻松切片。
这是一篇关于 numpy 切片的相关文章:Numpy - slicing 2d row or column vector from array
关于python - 如何为 tensorflow 建模数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43995397/