Introduction to TensorFlow¶
I found it very difficult to install on my M1 Mac computer. Maybe it will work better for you.
On most computers (including the lab computers), it should be enough to go to
Anaconda Navigator -> Environments -> Uninstalled ->
tensorflow
and then click Apply. It may take about 5 minutes.
Another option is to open a terminal and type pip install tensorflow
.
If you can’t get it installed on your computer, an alternative is to try running this notebook in Google Colab or on a website called Deepnote. I personally prefer Deepnote, but Google Colab is more famous and more optimized for TensorFlow.
If you get tired of the TensorFlow warning messages, you can hide most of them with the following code. It must be run before you import tensorflow. If you change 3 to 2 or 1 or 0, there will be more warning messages (with 0 having all the warning messages). Source
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
Init Plugin
Init Graph Optimizer
Init Kernel
model = keras.Sequential(
[
keras.layers.InputLayer(input_shape = (2,)),
keras.layers.Dense(1,activation="sigmoid")
]
)
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 1) 3
=================================================================
Total params: 3
Trainable params: 3
Non-trainable params: 0
_________________________________________________________________
model.get_weights()
[array([[ 1.0675725],
[-0.8307821]], dtype=float32),
array([0.], dtype=float32)]
# "mse"
model.compile(loss="binary_crossentropy",
optimizer="sgd",metrics=["accuracy"])
X = [[0,0],
[1,0],
[0,1],
[1,1]]
y = [0,1,1,1]
model.fit(X,y)
1/1 [==============================] - 0s 123ms/step - loss: 0.6907 - accuracy: 0.7500
2021-11-17 12:41:27.633026: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
<tensorflow.python.keras.callbacks.History at 0x160eb31c0>
model.fit(X,y,epochs=100)
Epoch 1/100
1/1 [==============================] - 0s 8ms/step - loss: 0.6891 - accuracy: 0.5000
Epoch 2/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6875 - accuracy: 0.5000
Epoch 3/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6859 - accuracy: 0.5000
Epoch 4/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6843 - accuracy: 0.5000
Epoch 5/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6828 - accuracy: 0.5000
Epoch 6/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6812 - accuracy: 0.5000
Epoch 7/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6797 - accuracy: 0.5000
Epoch 8/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6781 - accuracy: 0.5000
Epoch 9/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6766 - accuracy: 0.5000
Epoch 10/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6751 - accuracy: 0.5000
Epoch 11/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6736 - accuracy: 0.5000
Epoch 12/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6721 - accuracy: 0.5000
Epoch 13/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6706 - accuracy: 0.5000
Epoch 14/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6691 - accuracy: 0.5000
Epoch 15/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6677 - accuracy: 0.5000
Epoch 16/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6662 - accuracy: 0.5000
Epoch 17/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6648 - accuracy: 0.5000
Epoch 18/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6633 - accuracy: 0.5000
Epoch 19/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6619 - accuracy: 0.5000
Epoch 20/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6605 - accuracy: 0.5000
Epoch 21/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6590 - accuracy: 0.5000
Epoch 22/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6576 - accuracy: 0.5000
Epoch 23/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6563 - accuracy: 0.5000
Epoch 24/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6549 - accuracy: 0.5000
Epoch 25/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6535 - accuracy: 0.5000
Epoch 26/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6521 - accuracy: 0.5000
Epoch 27/100
1/1 [==============================] - 0s 4ms/step - loss: 0.6508 - accuracy: 0.5000
Epoch 28/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6494 - accuracy: 0.5000
Epoch 29/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6481 - accuracy: 0.5000
Epoch 30/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6467 - accuracy: 0.5000
Epoch 31/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6454 - accuracy: 0.5000
Epoch 32/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6441 - accuracy: 0.5000
Epoch 33/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6428 - accuracy: 0.5000
Epoch 34/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6415 - accuracy: 0.5000
Epoch 35/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6402 - accuracy: 0.5000
Epoch 36/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6389 - accuracy: 0.5000
Epoch 37/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6377 - accuracy: 0.5000
Epoch 38/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6364 - accuracy: 0.5000
Epoch 39/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6351 - accuracy: 0.5000
Epoch 40/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6339 - accuracy: 0.5000
Epoch 41/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6326 - accuracy: 0.5000
Epoch 42/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6314 - accuracy: 0.5000
Epoch 43/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6302 - accuracy: 0.5000
Epoch 44/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6290 - accuracy: 0.5000
Epoch 45/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6277 - accuracy: 0.5000
Epoch 46/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6265 - accuracy: 0.5000
Epoch 47/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6253 - accuracy: 0.5000
Epoch 48/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6242 - accuracy: 0.5000
Epoch 49/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6230 - accuracy: 0.5000
Epoch 50/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6218 - accuracy: 0.5000
Epoch 51/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6206 - accuracy: 0.5000
Epoch 52/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6195 - accuracy: 0.5000
Epoch 53/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6183 - accuracy: 0.5000
Epoch 54/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6172 - accuracy: 0.5000
Epoch 55/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6160 - accuracy: 0.5000
Epoch 56/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6149 - accuracy: 0.5000
Epoch 57/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6138 - accuracy: 0.5000
Epoch 58/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6127 - accuracy: 0.5000
Epoch 59/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6116 - accuracy: 0.5000
Epoch 60/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6104 - accuracy: 0.5000
Epoch 61/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6094 - accuracy: 0.5000
Epoch 62/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6083 - accuracy: 0.5000
Epoch 63/100
1/1 [==============================] - 0s 6ms/step - loss: 0.6072 - accuracy: 0.5000
Epoch 64/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6061 - accuracy: 0.5000
Epoch 65/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6050 - accuracy: 0.5000
Epoch 66/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6040 - accuracy: 0.5000
Epoch 67/100
1/1 [==============================] - 0s 7ms/step - loss: 0.6029 - accuracy: 0.5000
Epoch 68/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6019 - accuracy: 0.5000
Epoch 69/100
1/1 [==============================] - 0s 5ms/step - loss: 0.6008 - accuracy: 0.5000
Epoch 70/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5998 - accuracy: 0.5000
Epoch 71/100
1/1 [==============================] - 0s 6ms/step - loss: 0.5987 - accuracy: 0.5000
Epoch 72/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5977 - accuracy: 0.5000
Epoch 73/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5967 - accuracy: 0.5000
Epoch 74/100
1/1 [==============================] - 0s 7ms/step - loss: 0.5957 - accuracy: 0.5000
Epoch 75/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5947 - accuracy: 0.5000
Epoch 76/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5937 - accuracy: 0.5000
Epoch 77/100
1/1 [==============================] - 0s 6ms/step - loss: 0.5927 - accuracy: 0.5000
Epoch 78/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5917 - accuracy: 0.5000
Epoch 79/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5907 - accuracy: 0.5000
Epoch 80/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5897 - accuracy: 0.5000
Epoch 81/100
1/1 [==============================] - 0s 7ms/step - loss: 0.5888 - accuracy: 0.5000
Epoch 82/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5878 - accuracy: 0.5000
Epoch 83/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5868 - accuracy: 0.5000
Epoch 84/100
1/1 [==============================] - 0s 7ms/step - loss: 0.5859 - accuracy: 0.5000
Epoch 85/100
1/1 [==============================] - 0s 7ms/step - loss: 0.5849 - accuracy: 0.5000
Epoch 86/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5840 - accuracy: 0.5000
Epoch 87/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5831 - accuracy: 0.5000
Epoch 88/100
1/1 [==============================] - 0s 6ms/step - loss: 0.5821 - accuracy: 0.5000
Epoch 89/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5812 - accuracy: 0.5000
Epoch 90/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5803 - accuracy: 0.5000
Epoch 91/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5794 - accuracy: 0.5000
Epoch 92/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5784 - accuracy: 0.5000
Epoch 93/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5775 - accuracy: 0.5000
Epoch 94/100
1/1 [==============================] - 0s 6ms/step - loss: 0.5766 - accuracy: 0.5000
Epoch 95/100
1/1 [==============================] - 0s 6ms/step - loss: 0.5757 - accuracy: 0.5000
Epoch 96/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5749 - accuracy: 0.5000
Epoch 97/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5740 - accuracy: 0.5000
Epoch 98/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5731 - accuracy: 0.5000
Epoch 99/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5722 - accuracy: 0.5000
Epoch 100/100
1/1 [==============================] - 0s 5ms/step - loss: 0.5714 - accuracy: 0.5000
<tensorflow.python.keras.callbacks.History at 0x16259b4f0>
model.get_weights()
[array([[ 1.217262 ],
[-0.57473016]], dtype=float32),
array([0.18040945], dtype=float32)]
model.predict([[0,0]])
2021-11-17 12:44:21.663560: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
array([[0.54498047]], dtype=float32)
model.fit(X,y,epochs=1000,verbose=False)
<tensorflow.python.keras.callbacks.History at 0x162b70ee0>
model.predict([[0,0]])
array([[0.5626659]], dtype=float32)