from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasRegressor import numpy as np from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error from keras.callbacks import TensorBoard def model(optimizer="adam"): #create model model = Sequential() model.add(Dense(input_dim=4,units=12,activation="relu")) model.add(Dense(units=8,activation="relu")) model.add(Dense(units=1,activation="sigmoid")) #compile model model.compile(loss="mse",optimizer=optimizer,metrics=["accuracy"],) return model ####################################################################################### #create data np.random.seed(seed=10) X = np.random.randn(100,4) y = np.random.randn(100) #split data in train dataset and test dataset X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) #using wrappers to create sklearn interface model = KerasRegressor(build_fn=model,epochs=10,batch_size=5) #training #引入Tensorboard画图 model.fit(X_train,y_train,validation_split=0.3, callbacks=[TensorBoard(log_dir="H:/1/",histogram_freq=1)]) #predicting y_pred = model.predict(X_test) #evalution print("mse:"+str(mean_squared_error(y_test,y_pred)))
启动:tensorboard --logdir="H:/1/"