长短期记忆神经网络预测比特币,报错
收藏
#请先运行此代码块获得代码
!DATA_PATH=data/data1971/ && NEW_NAME=$(find -name *[0-9].ipynb) && NEW_NAME=${NEW_NAME%.*} && NEW_NAME=${NEW_NAME#./} && unzip -o ${DATA_PATH}Bitcoin_LSTM_fluid.zip && cp -rf Bitcoin_LSTM_fluid/. . && mv Bitcoin_fluid_lstm_complete.ipynb ${NEW_NAME}"_complete.ipynb"
import numpy as np
import math
import matplotlib.pyplot as plt
import paddle
import paddle.fluid as fluid
from __future__ import print_function
import os
import csv
import paddle.dataset.imdb as imdb
import sys
import gzip
import unittest
import contextlib
#调用数据库,并加载数据到数组data中
cur_dir = os.path.dirname(os.path.realpath("__file__"))
filename = cur_dir + "/datasets/BITEBI.csv"
with open(filename) as f:
reader = csv.reader(f)
data = []
for row in reader:
data.append([i for i in row[0].split(';')])
data = np.array(data, dtype='float32') #把string数组转换为float数组
#划分训练数据集和预测数据集
ratio = 0.8
data_num = len(data)
slice = int(ratio * data_num)
train_data = data[:slice]
test_data = data[slice:]
#归一化
def normalization(data):
global maximums,minimums,averages
maximums,minimums,averages = data.max(axis = 0),data.min(axis = 0),data.sum(axis = 0)/data.shape[0]
feature_number = 7
m = maximums - minimums
for index in range(feature_number):
data[:,index] = (data[:,index] - averages[index])/ m[index]
return data
train_data = normalization(train_data)
test_data = normalization(test_data)
#定义读取数据格式
def train_mydata():
def reader():
for d in train_data:
yield d[:-1], d[-1]
return reader
def test_mydata():
def reader():
for d in test_data:
yield d[:-1], d[-1]
return reader
#完成数据的最终处理
BATCH_SIZE = 50
# 设置train_reader
train_reader = paddle.batch( paddle.reader.shuffle ( train_mydata(), buf_size=1298 ), batch_size=BATCH_SIZE )
#设置测试 reader
test_reader = paddle.batch( paddle.reader.shuffle ( test_mydata(), buf_size=325 ), batch_size=BATCH_SIZE )
#定义长短期记忆神经网络
def lstm_net(data,label, emb_dim=6, hid_dim=6, hid_dim2=6, output_dim=6, emb_lr=30.0):
fc0 = fluid.layers.fc(input=data, size=hid_dim * 4)
lstm_h,c = fluid.layers.dynamic_lstm(input=fc0, size=hid_dim * 4, is_reverse=False)
lstm_max = fluid.layers.sequence_pool(input=lstm_h, pool_type='max')
lstm_max_tanh = fluid.layers.tanh(lstm_max)
fc1 = fluid.layers.fc(input=lstm_max_tanh, size=hid_dim2, act='tanh')
prediction = fluid.layers.fc(input=fc1, size=output_dim, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=label)
avg_cost = fluid.layers.mean(x=cost)
acc = fluid.layers.accuracy(input=prediction, label=label)
#返回值,平均损失,训练精度,预测值
return avg_cost, acc, prediction
#定义画图框架
from paddle.v2.plot import Ploter
train_title = "Train cost"
test_title = "Test cost"
plot_cost = Ploter(train_title, test_title)
#定义训练神经网络
def train(train_reader, network, use_cuda, save_dirname, lr, batch_size, pass_num):
data = fluid.layers.data(name='data', shape=[6], dtype='float32', lod_level=1)
label = fluid.layers.data(name='label', shape=[1], dtype='float32', lod_level= 1)
#model = lstm_net(x,dim_data)
avg_cost, acc, prediction = network(data, label)
# 定义优化器
sgd_optimizer = fluid.optimizer.Adam(learning_rate=0.001)
sgd_optimizer.minimize(avg_cost)
# 参数初始化
place = fluid.CPUPlace()
#place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
# place = fluid.CUDAPlace(1)
exe = fluid.Executor(place)#Python中的一个执行器,只支持单个GPU运行。
exe.run( fluid.default_startup_program() )#获取默认/全局启动程序,并由执行器执行
feeder = fluid.DataFeeder(feed_list=[data, label],place=place)
# 定义双层循环
step = 0
for pass_id in range(pass_num):
total_loss_pass = 0#初始化每一个epoch的损失值初始值为0
for data in train_reader(): #data表示batch大小的数据样本
avg_loss_value, = exe.run(
fluid.default_main_program(),
feed= feeder.feed(data),
fetch_list=[avg_cost])
total_loss_pass += avg_loss_value#计算每个epoch的总损失值
# 画图
plot_cost.append(train_title, step, avg_loss_value)
step += 1
plot_cost.plot()
fluid.io.save_inference_model(save_dirname, ['data','label'], [prediction], exe)
#执行训练
train(train_reader, lstm_net ,use_cuda=False, save_dirname="lstm_model", lr=0.05, batch_size=100, pass_num=100)
#以下报错代码
TypeErrorTraceback (most recent call last)
in ()
----> 1 train(train_reader, lstm_net ,use_cuda=False, save_dirname="lstm_model", lr=0.05, batch_size=100, pass_num=100)
in train(train_reader, network, use_cuda, save_dirname, lr, batch_size, pass_num)
22 avg_loss_value, = exe.run(
23 fluid.default_main_program(),
---> 24 feed= feeder.feed(data),
25 fetch_list=[avg_cost])
26 total_loss_pass += avg_loss_value#计算每个epoch的总损失值
/opt/conda/envs/py27-paddle0.11/lib/python2.7/site-packages/paddle/fluid/data_feeder.pyc in feed(self, iterable)
189 "len(feed_list) (%s)") % (len(each_sample), len(converter))
190 for each_converter, each_slot in six.zip(converter, each_sample):
--> 191 each_converter.feed(each_slot)
192 ret_dict = {}
193 for each_name, each_converter in six.zip(self.feed_names, converter):
/opt/conda/envs/py27-paddle0.11/lib/python2.7/site-packages/paddle/fluid/data_feeder.pyc in feed(self, data)
58
59 def feed(self, data):
---> 60 self._feed_impl_(data, self.lod, self.lod_level)
61
62 def _feed_impl_(self, data, lod, lod_level):
/opt/conda/envs/py27-paddle0.11/lib/python2.7/site-packages/paddle/fluid/data_feeder.pyc in _feed_impl_(self, data, lod, lod_level)
64 self.data.append(data)
65 else:
---> 66 lod[0].append(len(data))
67 for each_data in data:
68 self._feed_impl_(each_data, lod[1:], lod_level - 1)
TypeError: object of type 'numpy.float32' has no len()
0
收藏
请登录后评论
data_num = len(data)
这行代码有问题
我把切片函数改成这样还是说“object of type 'numpy.float32' has no len()”
ratio = 0.8
slice = int(ratio * 1623)
train_data = data[:slice]
test_data = data[slice:]