赞
踩
我总结的建立神经网络的具体步骤,和一些细节,以及提供案例仅供参考
数据集
需要预测的特征
导入数据集,将特征和目标分开存放
import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler import tensorflow as tf from keras import Sequential from keras.layers import Dense import pickle def load_x(filename): X = np.loadtxt(filename, delimiter=',') return X def load_y(filename): y = np.loadtxt(filename, delimiter=',') return y def eval_mse(y, yhat): m = len(y) err = (yhat - y) ** 2 err = np.sum(err) err = err / (2 * m) return (err) X = load_x("dataX.txt") y = load_y("datay.txt")
Xs = StandardScaler()
Xn = Xs.fit_transform(X)
X_new = np.tile(Xn, (20000, 1))
ys = StandardScaler()
yn = ys.fit_transform(y)
y_new = np.tile(yn, (20000, 1))
X_train, X_, y_train, y_ = train_test_split(X_new,y_new,test_size=0.4, random_state=1) X_cv, X_test, y_cv, y_test = train_test_split(X_,y_,test_size=0.50, random_state=1) # print(X_train.shape,y_train.shape) model = Sequential([ Dense(units=32,activation="relu",kernel_regularizer=tf.keras.regularizers.L2(0.1)), Dense(units=16, activation="relu", kernel_regularizer=tf.keras.regularizers.L2(0.1)), Dense(units=2) ]) model.compile( loss=tf.keras.losses.MeanSquaredError(), optimizer="adam" ) model.fit( X_train,y_train, epochs=10 )
"""
当前文件
"""
pickle_out = open("model.p", "wb")
pickle.dump(model, pickle_out)
pickle_out.close()
import numpy as np
from sklearn.model_selection import train_test_split
import pickle
from sklearn.preprocessing import StandardScaler
pickle_in = open("model.p", "rb")
model = pickle.load(pickle_in)
def load_x(filename): X = np.loadtxt(filename, delimiter=',') return X def load_y(filename): y = np.loadtxt(filename, delimiter=',') return y def eval_mse(y, yhat): m = len(y) err = (yhat - y) ** 2 err = np.sum(err) err = err / (2 * m) return (err) X = load_x("dataX.txt") y = load_y("datay.txt") Xs = StandardScaler() Xn = Xs.fit_transform(X) X_new = np.tile(Xn, (20000, 1)) ys = StandardScaler() yn = ys.fit_transform(y) y_new = np.tile(yn, (20000, 1)) X_train, X_, y_train, y_ = train_test_split(X_new,y_new,test_size=0.4, random_state=1) X_cv, X_test, y_cv, y_test = train_test_split(X_,y_,test_size=0.50, random_state=1)
filename = "dataX.txt" a = float(input("please Input One values: ")) b = float(input("please Input One values: ")) c = float(input("please Input One values: ")) with open(filename,'a') as file_object: file_object.write(f"\n{a},{b},{c}") X = load_x("dataX.txt") Xs_new = StandardScaler() Xn_new = Xs.fit_transform(X) x_norm = np.array([Xn_new[-1]]) y_pre = model.predict(x_norm) y_INV = ys.inverse_transform(y_pre) print(y_INV) filename= "datay.txt" with open(filename,'a') as file_object: file_object.write(f"\n{y_INV[0][0]},{y_INV[0][1]}")
s = model.evaluate(X_train, y_train, verbose=0)
print(s)
y_pre_cv = model.predict(X_cv)
err_cv = eval_mse(y_cv,y_pre_cv)
print(err_cv)
y_pre_train = model.predict(X_train)
err_train = eval_mse(y_train,y_pre_train)
print(err_train)
y_pre_test = model.predict(X_test)
y_pre = ys.inverse_transform(y_pre_test)
y_test = ys.inverse_transform(y_test)
print(f"Predictions of Test:\n{y_pre} \n True Values:\n{y_test}")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。