我是TF的新手,在重新构建张量时遇到麻烦,并且在运行线性回归模型时遇到了使用序列设置数组元素的错误。如何使用TF Learn重塑数据?
每行要素的外观如下:[0,1,0,0,0,0,0,0],[0,0,1,0,0],[0,1,0,0,0 ],[0,1,0,0,0],[0,0,0,1],1,[0,0,1]
#Load in csv file
data, labels = load_csv('data.csv', target_column=1, has_header=True,categorical_labels=True, n_classes=2)
#preprocess data
def preprocess(data, columns_to_ignore):
rank = 0
# Sort by descending id and delete columns
for id in sorted(columns_to_ignore, reverse=True):
[r.pop(id) for r in data]
for i in range(len(data)):
for j in range(len(data[i])):
if ',' in data[i][j]:
data[i][j] = data[i][j].split(',')
data[i][j] = np.array(data[i][j], dtype=np.float32)
if i == 0:
rank += len(data[i][j])
else:
data[i][j] = float(data[i][j])
if i == 0:
rank += 1
return {
"rank": rank,
"data": data
}
to_ignore=[0]
proccess = preprocess(data, to_ignore)
rank = proccess["rank"]
data = proccess["data"]
input_ = tflearn.input_data(shape=[None, 1, rank])
linear = tflearn.single_unit(input_)
regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',
metric='R2', learning_rate=0.01)
m = tflearn.DNN(regression)
# move to 500
m.fit(data, labels, n_epoch=50, show_metric=True, snapshot_epoch=False)