Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import time
import keras
import matplotlib.pyplot as plt
import numpy as np
from keras.layers import Dense, Activation
from keras.models import Sequential
x = np.arange(-100, 100, 0.5)
y = x ** 2
model = Sequential()
model.add(keras.layers.normalization.BatchNormalization(input_shape=(1,)))
model.add(Dense(200))
model.add(Activation('relu'))
model.add(Dense(50))
model.add(Activation('elu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
t1 = time.clock()
for i in range(100):
model.fit(x, y, epochs=1000, batch_size=len(x), verbose=0)
predictions = model.predict(x)
print(i, " ", np.mean(np.square(predictions - y)), " t: ", time.clock() - t1)
# plt.hold(False)
# plt.plot(x, y, 'b', x, predictions, 'r--')
# plt.hold(True)
# plt.ylabel('Y / Predicted Value')
# plt.xlabel('X Value')
# plt.title([str(i), " Loss: ", np.mean(np.square(predictions - y)), " t: ", str(time.clock() - t1)])
# plt.pause(0.001)
# plt.show()
model.save("./out.model")
#%%
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
#%%
batch_size = 128
num_classes = 10
epochs = 8000
X_train =np.array( [[1,0],[0,1],[1,1],[0,0]], "float32")
y_train=np.array( [[1],[1],[1],[0]], "float32")
#%%
def test_model(model):
model.summary()
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['binary_accuracy'])
model.fit(X_train,y_train, epochs=epochs,verbose=0)
return model
#%%
model = test_model(Sequential(
layers=[
Dense(32, input_dim=2, activation='relu'),
Dense(1, activation='sigmoid')
]
))
model.predict(X_train)
#%%
model.predict(X_train).round()
#%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.