strongly improve the neural network
This commit is contained in:
parent
9f822df29d
commit
e155d9ef96
1 changed files with 24 additions and 16 deletions
40
kerastest.py
40
kerastest.py
|
@ -8,22 +8,24 @@ from keras.engine.saving import load_model
|
|||
from keras.layers import Dense
|
||||
from keras.utils import plot_model
|
||||
from matplotlib import pyplot as plt
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
from CustomScaler import CustomScaler
|
||||
from simulation_list import SimulationList
|
||||
|
||||
simulations = SimulationList.jsonlines_load()
|
||||
|
||||
random.shuffle(simulations.simlist)
|
||||
train_data = [s for s in simulations.simlist]
|
||||
test_data = [s for s in simulations.simlist if s.type != "original"]
|
||||
|
||||
train_data = set([s for s in simulations.simlist])
|
||||
new_data = [s for s in simulations.simlist if s.type != "original"]
|
||||
random.seed(1)
|
||||
test_data = set(random.sample(new_data, int(len(new_data) * 0.2)))
|
||||
train_data -= test_data
|
||||
print(len(train_data), len(test_data))
|
||||
X = np.array(
|
||||
[[s.alpha, s.v, s.projectile_mass, s.gamma, s.target_water_fraction, s.projectile_water_fraction] for s in
|
||||
train_data])
|
||||
scaler = StandardScaler()
|
||||
scaler = CustomScaler()
|
||||
scaler.fit(X)
|
||||
x = scaler.transform(X)
|
||||
x = scaler.transform_data(X)
|
||||
print(x.shape)
|
||||
Y = np.array([s.water_retention_both for s in train_data])
|
||||
print(Y.shape)
|
||||
|
@ -31,30 +33,36 @@ X_test = np.array(
|
|||
[[s.alpha, s.v, s.projectile_mass, s.gamma, s.target_water_fraction, s.projectile_water_fraction] for s in
|
||||
test_data])
|
||||
Y_test = np.array([s.mass_retention_both for s in test_data])
|
||||
x_test = scaler.transform(X_test)
|
||||
x_test = scaler.transform_data(X_test)
|
||||
# print(X_test)
|
||||
# print(X[0])
|
||||
# exit()
|
||||
tbCallBack = keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=32, write_graph=True,
|
||||
write_grads=False, write_images=False, embeddings_freq=0,
|
||||
random.seed()
|
||||
tbCallBack = keras.callbacks.TensorBoard(log_dir='./logs/{}'.format(random.randint(0, 100)), histogram_freq=1,
|
||||
batch_size=32, write_graph=True,
|
||||
write_grads=True, write_images=True, embeddings_freq=0,
|
||||
embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None,
|
||||
update_freq='epoch')
|
||||
|
||||
if os.path.exists("model.hd5") and False:
|
||||
if os.path.exists("model.hd5"):
|
||||
model = load_model("model.hd5")
|
||||
else:
|
||||
|
||||
model = Sequential()
|
||||
model.add(Dense(6, input_dim=6, kernel_initializer='normal', activation='relu'))
|
||||
model.add(Dense(6, input_dim=6, activation='relu'))
|
||||
model.add(Dense(4, kernel_initializer='normal', activation='relu'))
|
||||
model.add(Dense(1, kernel_initializer='normal'))
|
||||
model.add(Dense(3, kernel_initializer='normal', activation='relu'))
|
||||
model.add(Dense(1, kernel_initializer='normal', activation="sigmoid"))
|
||||
model.compile(loss='mean_squared_error', optimizer='adam')
|
||||
|
||||
model.summary()
|
||||
plot_model(model, "model.png", show_shapes=True, show_layer_names=True)
|
||||
model.fit(x, Y, epochs=400, callbacks=[tbCallBack], validation_split=0.02)
|
||||
model.fit(x, Y, epochs=200, callbacks=[tbCallBack], validation_data=(x_test, Y_test))
|
||||
loss = model.evaluate(x_test, Y_test)
|
||||
print(loss)
|
||||
if loss > 0.04:
|
||||
# exit()
|
||||
...
|
||||
# print("-------------------------------------")
|
||||
# exit()
|
||||
model.save("model.hd5")
|
||||
|
@ -62,14 +70,14 @@ else:
|
|||
xrange = np.linspace(-0.5, 60.5, 100)
|
||||
yrange = np.linspace(0.5, 5.5, 100)
|
||||
xgrid, ygrid = np.meshgrid(xrange, yrange)
|
||||
mcode = 23e10
|
||||
mcode = 1e23
|
||||
wpcode = 15 / 100
|
||||
wtcode = 15 / 100
|
||||
gammacode = 0.7
|
||||
testinput = np.array([[np.nan, np.nan, mcode, gammacode, wtcode, wpcode]] * 100 * 100)
|
||||
testinput[::, 0] = xgrid.flatten()
|
||||
testinput[::, 1] = ygrid.flatten()
|
||||
testinput = scaler.transform(testinput)
|
||||
testinput = scaler.transform_data(testinput)
|
||||
|
||||
print(testinput)
|
||||
print(testinput.shape)
|
||||
|
|
Reference in a new issue