1

我有以下形式的数据:

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:无法为具有形状(3, 6)的张量'InputData/X:0'提供形状值'(?, 4)'

我猜这是因为我的输入数据有 7 列(0...6),但我希望输入层仅将前四列作为输入,并预测数据中的最后 3 列作为输出。我该如何建模?

4

1 回答 1

3

如果数据采用 numpy 格式,则前 4 列采用简单切片:

data[:,0:4]

表示“:所有行”,0:4是一个值范围0,1,2,3,前 4 列。

如果数据不是 numpy 格式,只需将其转换为 numpy 格式,以便您轻松切片。

这是关于 numpy 切片的相关文章:Numpy - 从数组中切片 2d 行或列向量

于 2017-05-16T16:10:17.623 回答