The import numpy as np
The from pandas import DataFrame, read_csv
The import matplotlib. Pyplot as PLT
# the from keras. Utils. Visualize_util import plot
The import within DNN
Def select_input_datasets (data, y, x, step) :
Inputs=data [x]. Values. Tolist ()
New_input_sets=[]
For I in range (len (inputs) :
X=[] # 2 d
For j in range (step 1, 1, 1) :
If (I - j) & lt; 0:
X.a ppend ([0] * len (inputs [0]))
The else:
X.a ppend (inputs (I - j])
New_input_sets. Append (x)
# print np. Array (new_input_sets). Shape
Return {' y ': np. Array (data [y] values),' x ': np. The array (new_input_sets)}
Def read_data (train_file_name) :
Print (" The "train" The data is loading ")
Return read_csv (train_file_name)
Def training (train_data_name=None, time_step=None, x_variables=None, target=None,
Dim_hidden_layer=None, lr=None, loss=None, batch_size=None, nb_epoch=None, validation_split=None,
Best_model_weight_name=None, last_model_weight_name=None) :
Original_train_data=https://bbs.csdn.net/topics/read_data (train_data_name)
Train_input=select_input_datasets (original_train_data,
Y=target,
X=x_variables,
Step=time_step)
DNN_model=within DNN. Within DNN (train_input)
# build a model within DNN
DNN_model. Build_model (dim_hidden_layer=dim_hidden_layer, lr=lr, loss=loss)
DNN_model. Shows_model ()
# fit the model "according to the training data
DNN_model. Fit_model (batch_size=batch_size, nb_epoch=nb_epoch, validation_split=validation_split, best_model_weight_name=best_model_weight_name)
DNN_model. Save_model_to_file (last_model_weight_name)
DNN_model. Predict (train_input, show=True)
DNN_model. Evaluate (train_input)
Return DNN_model
Def testing (test_data_name=None, time_step=None, x_variables=None, target=None,
Restore_model_weight_name=None) :
Original_test_data=https://bbs.csdn.net/topics/read_data (test_data_name)
Testing_input=select_input_datasets (original_test_data,
Y=target,
X=x_variables,
Step=time_step)
Testing_input [' x ']=testing_input [' x '] [0:]
Testing_input [' y ']=testing_input [' y '] [0:]
DNN_model=within DNN. Within DNN (testing_input)
DNN_model. Restore_model_from_file (restore_model_weight_name)
DNN_model. Predict (testing_input, show=True)
DNN_model. Evaluate (testing_input)
Return DNN_model
If __name__=="__main__" :
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# training or testing
Only_test=False
A='0'
# 'Best_model. Weights' or' last_model '
Restore_model_weight_name='C: \ \ Users \ \ Iron \ \ Desktop \ \ 0 \ \ best_model weights'
# data
Train_data_name='C: \ \ Users \ \ Iron \ \ Desktop \ \ 0 \ \ train_data CSV'
Test_data_name='C: \ \ Users \ \ Iron \ \ Desktop \ \ 0 \ \ test_data CSV'
Target='w'
X_variables=[
A1, a2, a3 ", "a4",
"A6," "a6", "a7, a8,
"A10" a9 ", "
]
# model param
Time_step=2
Dim_hidden_layer=64
Lr=0.001
Loss='mape'
# fit param
Batch_size=2
Nb_epoch=15000
Validation_split=0.05
# save
Best_model_weight_name='C: \ \ Users \ \ Iron \ \ Desktop \ \ 0 \ \ best_model weights'
Last_model_weight_name='C: \ \ Users \ \ Iron \ \ Desktop \ \ 0 \ \ last_model weights'
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
If not only_test:
The model=training (train_data_name=train_data_name,
Time_step=time_step,
X_variables=x_variables, target=target,
Dim_hidden_layer=dim_hidden_layer, lr=lr, loss=loss,
Batch_size=batch_size, nb_epoch=nb_epoch, validation_split=validation_split,
Best_model_weight_name=best_model_weight_name,
Last_model_weight_name=last_model_weight_name)
If only_test:
The model=testing (test_data_name=test_data_name,
Time_step=time_step,
X_variables=x_variables, target=target,
Restore_model_weight_name=restore_model_weight_name,)
CodePudding user response:
A function need a units=XXX variables, but you didn't give in,Should there will be a number of lines in an error message,