如何为张量流建模数据?

How to model data for tensorflow?

我有以下形式的数据:

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]

:表示"all rows",0:4是一个数值范围0,1,2,3,前4列。

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

这是一篇关于 numpy 切片的相关文章: