In [0]:
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
In [0]:
X = [[0, 0], [0, 1], [1, 0], [1, 1]]
y = [0, 1, 1, 0]
In [0]:
In [72]:
# sgd cant do this with 1 node
import tensorflow as tf
import numpy as np
X = np.array(X)
y = np.array(y)
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(2, activation='relu'),
])
sgd = tf.keras.optimizers.SGD(learning_rate=0.1)
model.compile(optimizer= sgd,
loss='mean_squared_error',
metrics=['mean_squared_error'])
model.fit(X, y, epochs=1000, verbose=0)
_, acc = model.evaluate(X, y)
print('acc = ' + str(acc))
In [77]:
import tensorflow as tf
import numpy as np
X = np.array(X)
y = np.array(y)
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(2, activation='tanh', input_shape=(2,)),
tf.keras.layers.Dense(1, activation='sigmoid'),
])
sgd = tf.keras.optimizers.SGD(learning_rate=0.1)
model.compile(optimizer=sgd,
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(X, y, batch_size = 4, epochs=10000, verbose=0)
_, acc = model.evaluate(X, y)
print(model.predict_proba(X))
print(model.get_weights())
print(model.predict(X,batch_size=4))
print('acc = ' + str(acc))
In [0]:
In [81]:
#adam much better but still 100
import tensorflow as tf
import numpy as np
X = np.array(X)
y = np.array(y)
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(2, activation='tanh', input_shape=(2,)),
tf.keras.layers.Dense(1, activation='sigmoid'),
])
sgd = tf.keras.optimizers.Adam(learning_rate=0.1)
model.compile(optimizer=sgd,
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(X, y, batch_size = 4, epochs=100, verbose=1)
_, acc = model.evaluate(X, y)
print(model.predict_proba(X))
print(model.get_weights())
print(model.predict(X,batch_size=4))
print('acc = ' + str(acc))
In [0]: