4 ms·
* Fix the data. Right now the optimal coefficients on your data (using least-squares) are m=1.79794911, b=31.952525636156476, which yields 211.74743638 when pre
by czr 7y ago
* Fix the data. Right now the optimal coefficients on your data (using least-squares) are m=1.79794911, b=31.952525636156476, which yields 211.74743638 when predicting on 100.
* Tune the hyperparameters. In particular, tune the learning rate. To quote the Deep Learning Book [0]:
> The learning rate is perhaps the most important hyperparameter. If you have time to tune only one hyperparameter, tune the learning rate. It controls the effective capacity of the model in a more complicated way than other hyperparameters—the effective capacity of the model is highest when the learning rate is correct for the optimization problem, not when the learning rate is especially large or especially small.
The following code will yield exactly 212 almost every run (using fixed data and a different choice of learning rate):
```
celsius_q = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float)
fahrenheit_a = np.array([x * 1.8 + 32 for x in celsius_q], dtype=float)
for i, c in enumerate(celsius_q):
print("{} degrees Celsius = {} degrees Fahrenheit".format(c, fahrenheit_a[i]))
l0 = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([l0])
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(lr=1.0))
history = model.fit(celsius_q, fahrenheit_a, epochs=500, verbose=False)
print("Finished training the model")
print(model.predict([100.0]))
```
[0] https://www.deeplearningbook.org/contents/guidelines.html https://www.deeplearningbook.org/contents/guidelines.html
- tylerhou 7y agoCode blocks don't work on HN; you need to format all your code with spaces: celsius_q = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float) fahrenheit_a = np.array([x * 1.8 + 32 for x in celsius_q], dtype=float) for i, c in enumerate(celsius_q): print("{} degrees Celsius = {} degrees Fahrenheit".format(c, fahrenheit_a[i])) l0 = tf.keras.layers.Dense(units=1, input_shape=[1]) model = tf.keras.Sequential([l0]) model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(lr=1.0)) history = model.fit(celsius_q, fahrenheit_a, epochs=500, verbose=False) print("Finished training the model") print(model.predict([100.0]))