简体   繁体   English

Python神经网络代码(Keras)的问题

[英]Issue with Python Neural Network Code (Keras)

I want to build an neural net model where say the data of 100 rows is divided into 5 stacks of 20 rows each, now rather than iterating through it all at once I want to build a neural net based on the first 20 rows of data (ie first stack), then save the model(weights etc) and then pass the next stack (Next 20 rows ie row no 21-40) into the updated model (ie weights updated from previous model) rather and so on. 我想建立一个神经网络模型,其中将100行的数据分为5个堆栈,每个堆栈有20行,而不是一次遍历所有的堆栈,我想基于前20行数据来构建神经网络(例如,第一个堆栈),然后保存模型(权重等),然后将下一个堆栈(接下来的20行,即第21-40行)传递到更新的模型中(即,从先前模型中更新的权重),依此类推。 Can someone tell me what such type of neural network is called? 有人可以告诉我这种神经网络的类型吗? I just tried my first neural net yesterday where I iterated all data in batches (which I believe takes place in one epoch rather than multiple). 昨天我刚刚尝试了我的第一个神经网络,在该网络中我批量访问了所有数据(我相信这是在一个时期而不是多个时期内进行的)。

Following is the Neural Net I made in Python using Keras(Tensorflow backend) can someone suggest me edits to make the following model as per my requirement? 以下是我使用Keras(Tensorflow后端)在Python中制作的神经网络,有人可以建议我根据我的要求进行编辑以制作以下模型吗?

# Create first network with Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy
import pandas as pd
# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

# load dataset
raw_data = pd.read_excel('Data.xlsx',sep=',')
df = raw_data.iloc[:,0:2]

df = pd.get_dummies(df)
rows,cols = df.shape
output_dim = 7 # No. of Output Dimensions/Categories

#Splitting Data in Training & Testing
X_train,X_test,y_train,y_test = train_test_split(df.iloc[:,0:cols-output_dim],df.iloc[:,cols-output_dim:cols],test_size=0.2,random_state=seed)

X  = X_train.as_matrix()
X_test = X_test.as_matrix()
Y = y_train.as_matrix()
Y_test = y_test.as_matrix()


# create model
model = Sequential()
model.add(Dense(X.shape[1], input_dim=X.shape[1], activation='relu')) #Input Layer
model.add(Dense(X.shape[1], activation='relu')) #Hidden Layer
model.add(Dense(X.shape[1], activation='relu')) #Hidden Layer
model.add(Dense(X.shape[1], activation='relu')) #Hidden Layer
model.add(Dense(output_dim, activation='softmax')) #Output Layer

# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Fit the model
model.fit(X,Y,nb_epoch=10, validation_data=(X_test,Y_test), batch_size=83,verbose=1)

# evaluate the model
loss, accuracy = model.evaluate(X1, Y1)
print("\nValidation Data [Loss: %.2f, Accuracy: %.2f%%]" % (loss, accuracy*100))

It sounds like you want to train on your data in minibatches of size 20, and save the model after each minibatch. 听起来您想在大小为20的微型批次中训练数据,并在每次微型批次之后保存模型。 You don't need to change the shape of your input data for this - a matrix of shape (nb_datapoints, nb_features) works. 您无需为此更改输入数据的形状-形状矩阵(nb_datapoints,nb_features)起作用。 Make sure to specify batch_size=20 when you call model.fit() . 调用model.fit()时,请确保指定batch_size=20

In order to save your model after each minibatch, look into Keras callbacks . 为了在每次迷你批处理之后保存模型,请查看Keras 回调 You'd need to write your own custom callback, but you can model it after the existing ModelCheckpoint callback - that saves the model after each epoch, so it should be relatively simple to customize it for your needs. 您需要编写自己的自定义回调,但是您可以在现有ModelCheckpoint回调之后ModelCheckpoint进行建模-在每个时期之后保存模型,因此根据您的需求自定义它应该相对简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM