Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Keras LSTM网络中使批量大小大于1?_Python_Tensorflow_Keras_Lstm - Fatal编程技术网

Python 如何在Keras LSTM网络中使批量大小大于1?

Python 如何在Keras LSTM网络中使批量大小大于1?,python,tensorflow,keras,lstm,Python,Tensorflow,Keras,Lstm,我一直在使用批量大小为1的Keras训练LSTM,速度非常慢。我想增加批量,以加快培训时间,但我不知道怎么做 下面是显示我的问题的代码(我的最小、可复制的示例)。它的批处理大小为1,但如何使用批处理大小为2 import pandas as pd import numpy as np from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from kera

我一直在使用批量大小为1的Keras训练LSTM,速度非常慢。我想增加批量,以加快培训时间,但我不知道怎么做

下面是显示我的问题的代码(我的最小、可复制的示例)。它的批处理大小为1,但如何使用批处理大小为2

import pandas as pd
import numpy as np

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import TimeDistributed

#the demo data contains 10 instances each with 7 features and 3 targets
#the features are 0 or 1, the targets are the sum of the features in binary (with 3 bits)
raw_data = [[1,1,1,0,0,0,1,1,0,0], #has features 1,1,1,0,0,0,1, which contains 4 (100 in binary) 1's
            [0,0,0,0,0,0,0,0,0,0],[0,1,1,0,0,0,0,0,1,0],[1,0,0,1,1,1,1,1,0,1],[0,0,0,0,0,1,0,0,0,1],
            [1,1,1,1,1,1,0,1,1,0],[1,1,1,0,0,0,0,0,1,1],[1,1,1,1,1,1,1,1,1,1],[0,1,0,0,1,0,1,0,1,1],
            [1,1,1,1,0,1,1,1,1,0]]

#how can I use a batch_size of 2?
batch_size = 1
epochs = 10

df = pd.DataFrame(raw_data)
train_x, train_y =  df.values[:,0:-3], df.values[:,-3:]

#reshape <batch_size, time_steps, seq_len>  https://mc.ai/understanding-input-and-output-shape-in-lstm-keras/
#but can't reshape to a batch size of 2 as I get the ValueError below,
#ValueError: cannot reshape array of size 70 into shape (2,10,7) - which makes sense
#if I remove the batch_size from the reshape I get the ValueError below,
#ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (10, 7)
train_x = np.reshape(train_x, (batch_size, train_x.shape[0], train_x.shape[1]))
train_y = np.reshape(train_y, (batch_size, train_y.shape[0], train_y.shape[1]))

model = Sequential()
model.add(LSTM(batch_input_shape=(batch_size, 10, 7), return_sequences=True, units=7))
model.add(TimeDistributed(Dense(activation='linear', units=3)))
model.compile(loss='mse', optimizer='rmsprop')

#training and testing on the same data here, but it's only example code to demonstrate my batch_size problem
history = model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, validation_data=(train_x, train_y))

yhat = model.predict(train_x, batch_size)
print(yhat)
但这给出了一个错误的值

ValueError: Error when checking input: expected lstm_1_input to have shape (1, 7) but got array with shape (5, 7)
应用
model.fit时


在我上面的最小可复制示例中,我如何修改它以使用2的批量

仅在
型号中使用
批量大小
。以下代码适用于我:

import pandas as pd
import numpy as np

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import TimeDistributed

#the demo data contains 10 instances each with 7 features and 3 targets
#the features are 0 or 1, the targets are the sum of the features in binary (with 3 bits)
raw_data = [[1,1,1,0,0,0,1,1,0,0], #has features 1,1,1,0,0,0,1, which contains 4 (100 in binary) 1's
            [0,0,0,0,0,0,0,0,0,0],[0,1,1,0,0,0,0,0,1,0],[1,0,0,1,1,1,1,1,0,1],[0,0,0,0,0,1,0,0,0,1],
            [1,1,1,1,1,1,0,1,1,0],[1,1,1,0,0,0,0,0,1,1],[1,1,1,1,1,1,1,1,1,1],[0,1,0,0,1,0,1,0,1,1],
            [1,1,1,1,0,1,1,1,1,0]]

#how can I use a batch_size of 2?
batch_size = 2
epochs = 10

df = pd.DataFrame(raw_data)
train_x, train_y =  df.values[:,0:-3], df.values[:,-3:]

#reshape <batch_size, time_steps, seq_len>  https://mc.ai/understanding-input-and-output-shape-in-lstm-keras/
#but can't reshape to a batch size of 2 as I get the ValueError below,
#ValueError: cannot reshape array of size 70 into shape (2,10,7) - which makes sense
#if I remove the batch_size from the reshape I get the ValueError below,
#ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (10, 7)
train_x = np.reshape(train_x, (1, train_x.shape[0], train_x.shape[1]))
train_y = np.reshape(train_y, (1, train_y.shape[0], train_y.shape[1]))

model = Sequential()
model.add(LSTM(batch_input_shape=(1, 10, 7), return_sequences=True, units=7))
model.add(TimeDistributed(Dense(activation='linear', units=3)))
model.compile(loss='mse', optimizer='rmsprop')

#training and testing on the same data here, but it's only example code to demonstrate my batch_size problem
history = model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, validation_data=(train_x, train_y))

yhat = model.predict(train_x, 1)
print(yhat)
将熊猫作为pd导入
将numpy作为np导入
从keras.models导入顺序
从keras.layers导入稠密
从keras.layers导入LSTM
从keras.layers导入TimeDistributed
#演示数据包含10个实例,每个实例具有7个功能和3个目标
#特征为0或1,目标为二进制特征的总和(3位)
原始#数据=[[1,1,1,0,0,1,1,0,0],#具有特征1,1,1,0,0,1,其中包含4个(二进制为100个)1
[0,0,0,0,0,0,0,0,0,0],[0,1,1,0,0,0,0,0,1,0],[1,0,0,1,1,1,1,1,0,1],[0,0,0,0,0,1,0,0,0,1],
[1,1,1,1,1,1,0,1,1,0],[1,1,1,0,0,0,0,0,1,1],[1,1,1,1,1,1,1,1,1,1],[0,1,0,0,1,0,1,0,1,1],
[1,1,1,1,0,1,1,1,1,0]]
#如何使用2号批次?
批量大小=2
纪元=10
df=pd.DataFrame(原始数据)
列x,列y=df.值[:,0:-3],df.值[:,-3:]
#重塑https://mc.ai/understanding-input-and-output-shape-in-lstm-keras/
#但无法将批量大小改为2,因为我得到下面的ValueError,
#ValueError:无法将大小为70的数组重塑为形状(2,10,7)-这是有意义的
#如果我从重塑中删除批次大小,我会得到下面的ValueError,
#ValueError:检查输入时出错:预期lstm_1_输入有3个维度,但得到了形状为(10,7)的数组
列车x=np.整形(列车x,(1,列车x.形状[0],列车x.形状[1]))
序列y=np.整形(序列y,(1,序列y.形状[0],序列y.形状[1]))
模型=顺序()
添加(LSTM(批处理输入形状=(1,10,7),返回序列=真,单位=7))
添加(时间分布(密集(激活=线性),单位=3)))
compile(loss='mse',optimizer='rmsprop')
#在这里对相同的数据进行培训和测试,但这只是演示批量大小问题的示例代码
历史=模型.fit(序列x,序列y,年代=年代,批量大小=批量大小,验证数据=(序列x,序列y))
yhat=模型预测(列车×1)
印刷品(yhat)

仅在
型号中使用
批量大小。以下代码适用于我:

import pandas as pd
import numpy as np

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import TimeDistributed

#the demo data contains 10 instances each with 7 features and 3 targets
#the features are 0 or 1, the targets are the sum of the features in binary (with 3 bits)
raw_data = [[1,1,1,0,0,0,1,1,0,0], #has features 1,1,1,0,0,0,1, which contains 4 (100 in binary) 1's
            [0,0,0,0,0,0,0,0,0,0],[0,1,1,0,0,0,0,0,1,0],[1,0,0,1,1,1,1,1,0,1],[0,0,0,0,0,1,0,0,0,1],
            [1,1,1,1,1,1,0,1,1,0],[1,1,1,0,0,0,0,0,1,1],[1,1,1,1,1,1,1,1,1,1],[0,1,0,0,1,0,1,0,1,1],
            [1,1,1,1,0,1,1,1,1,0]]

#how can I use a batch_size of 2?
batch_size = 2
epochs = 10

df = pd.DataFrame(raw_data)
train_x, train_y =  df.values[:,0:-3], df.values[:,-3:]

#reshape <batch_size, time_steps, seq_len>  https://mc.ai/understanding-input-and-output-shape-in-lstm-keras/
#but can't reshape to a batch size of 2 as I get the ValueError below,
#ValueError: cannot reshape array of size 70 into shape (2,10,7) - which makes sense
#if I remove the batch_size from the reshape I get the ValueError below,
#ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (10, 7)
train_x = np.reshape(train_x, (1, train_x.shape[0], train_x.shape[1]))
train_y = np.reshape(train_y, (1, train_y.shape[0], train_y.shape[1]))

model = Sequential()
model.add(LSTM(batch_input_shape=(1, 10, 7), return_sequences=True, units=7))
model.add(TimeDistributed(Dense(activation='linear', units=3)))
model.compile(loss='mse', optimizer='rmsprop')

#training and testing on the same data here, but it's only example code to demonstrate my batch_size problem
history = model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, validation_data=(train_x, train_y))

yhat = model.predict(train_x, 1)
print(yhat)
将熊猫作为pd导入
将numpy作为np导入
从keras.models导入顺序
从keras.layers导入稠密
从keras.layers导入LSTM
从keras.layers导入TimeDistributed
#演示数据包含10个实例,每个实例具有7个功能和3个目标
#特征为0或1,目标为二进制特征的总和(3位)
原始#数据=[[1,1,1,0,0,1,1,0,0],#具有特征1,1,1,0,0,1,其中包含4个(二进制为100个)1
[0,0,0,0,0,0,0,0,0,0],[0,1,1,0,0,0,0,0,1,0],[1,0,0,1,1,1,1,1,0,1],[0,0,0,0,0,1,0,0,0,1],
[1,1,1,1,1,1,0,1,1,0],[1,1,1,0,0,0,0,0,1,1],[1,1,1,1,1,1,1,1,1,1],[0,1,0,0,1,0,1,0,1,1],
[1,1,1,1,0,1,1,1,1,0]]
#如何使用2号批次?
批量大小=2
纪元=10
df=pd.DataFrame(原始数据)
列x,列y=df.值[:,0:-3],df.值[:,-3:]
#重塑https://mc.ai/understanding-input-and-output-shape-in-lstm-keras/
#但无法将批量大小改为2,因为我得到下面的ValueError,
#ValueError:无法将大小为70的数组重塑为形状(2,10,7)-这是有意义的
#如果我从重塑中删除批次大小,我会得到下面的ValueError,
#ValueError:检查输入时出错:预期lstm_1_输入有3个维度,但得到了形状为(10,7)的数组
列车x=np.整形(列车x,(1,列车x.形状[0],列车x.形状[1]))
序列y=np.整形(序列y,(1,序列y.形状[0],序列y.形状[1]))
模型=顺序()
添加(LSTM(批处理输入形状=(1,10,7),返回序列=真,单位=7))
添加(时间分布(密集(激活=线性),单位=3)))
compile(loss='mse',optimizer='rmsprop')
#在这里对相同的数据进行培训和测试,但这只是演示批量大小问题的示例代码
历史=模型.fit(序列x,序列y,年代=年代,批量大小=批量大小,验证数据=(序列x,序列y))
yhat=模型预测(列车×1)
印刷品(yhat)

太好了,很乐意帮忙!太好了,很高兴帮忙!