Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1f21255

Browse files
committed
βž• Add Code
1 parent f9b9032 commit 1f21255

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

β€ŽCode/Example_Code_with_Comments.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import tensorflow as tf # deep learning library. Tensors are just multi-dimensional arrays
2+
3+
mnist = tf.keras.datasets.mnist # mnist is a dataset of 28x28 images of handwritten digits and their labels
4+
(x_train, y_train),(x_test, y_test) = mnist.load_data() # unpacks images to x_train/x_test and labels to y_train/y_test
5+
6+
x_train = tf.keras.utils.normalize(x_train, axis=1) # scales data between 0 and 1
7+
x_test = tf.keras.utils.normalize(x_test, axis=1) # scales data between 0 and 1
8+
9+
model = tf.keras.models.Sequential() # a basic feed-forward model
10+
model.add(tf.keras.layers.Flatten()) # takes our 28x28 and makes it 1x784
11+
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) # a simple fully-connected layer, 128 units, relu activation
12+
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) # a simple fully-connected layer, 128 units, relu activation
13+
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax)) # our output layer. 10 units for 10 classes. Softmax for probability distribution
14+
15+
model.compile(optimizer='adam', # Good default optimizer to start with
16+
loss='sparse_categorical_crossentropy', # how will we calculate our "error." Neural network aims to minimize loss.
17+
metrics=['accuracy']) # what to track
18+
19+
model.fit(x_train, y_train, epochs=3) # train the model
20+
21+
val_loss, val_acc = model.evaluate(x_test, y_test) # evaluate the out of sample data with model
22+
print(val_loss) # model's loss (error)
23+
print(val_acc) # model's accuracy

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /