Skip to content

Instantly share code, notes, and snippets.

@gangchen
Last active March 24, 2024 13:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save gangchen/4e75744b811f41ca4120ac8c296903e0 to your computer and use it in GitHub Desktop.
Save gangchen/4e75744b811f41ca4120ac8c296903e0 to your computer and use it in GitHub Desktop.
Tensorflow version for *Machine Learning for Beginners: An Introduction to Neural Networks*
import tensorflow as tf
import numpy as np
data = np.array([
[-2.0, -1], # Alice
[25, 6], # Bob
[17, 4], # Charlie
[-15, -6], # Diana
])
all_y_trues = np.array([
1, # Alice
0, # Bob
0, # Charlie
1, # Diana
])
inputs = tf.keras.Input(shape=(2,))
x = tf.keras.layers.Dense(2, use_bias=True)(inputs)
outputs = tf.keras.layers.Dense(1, use_bias=True, activation='sigmoid')(x)
m = tf.keras.Model(inputs, outputs)
m.compile(tf.keras.optimizers.SGD(learning_rate=0.1), 'mse')
m.fit(data, all_y_trues, epochs=1000, batch_size=1, verbose=0)
emily = np.array([[-7, -3]])
frank = np.array([[20, 2]])
print(m.predict(emily))
print(m.predict(frank))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment