-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensorflow_basics.py
47 lines (40 loc) · 1.56 KB
/
tensorflow_basics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
"""Tensorflow basics
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1BrPOQaFsQ0L17EKvBuSPuQiqEDHW7zw_
"""
!pip install -U tensorflow_datasets
import tensorflow as tf
from future import *
import tensorflow_datasets as tfds
tf.logging.set_verbosity(tf.logging.ERROR)
import math
import matplotlib as plt
import numpy as np
import tqdm
import tqdm.auto
tqdm.tqdm=tqdm.auto.tqdm
#print(tf.__version__)
tf.enable_eager_execution()
def normalise(images,label):
images=tf.cast(images,tf.float32)
images/=255
return images,label
dataset,metadata=tfds.load('fashion_mnist',as_supervised=True,with_info=True)
trainds,testds=dataset['train'],dataset['test']
trainds=trainds.map(normalise)
testds=testds.map(normalise)
num_train_examples=metadata.splits['train'].num_examples
num_test_examples=metadata.splits['test'].num_examples
model= tf.keras.Sequential([tf.keras.layers.Flatten(input_shape=(28,28,1)),
tf.keras.layers.Dense(128,activation=tf.nn.relu),
tf.keras.layers.Dense(10,activation=tf.nn.softmax)
])
BATCHSIZE=36
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
trainds=trainds.batch(BATCHSIZE).repeat().shuffle(num_train_examples)
testds=testds.batch(BATCHSIZE)
model.fit(trainds, epochs=5,steps_per_epoch= math.ceil(num_train_examples/BATCHSIZE))
test_loss,test_accu=model.evaluate(testds,steps=math.ceil(num_test_examples/BATCHSIZE))
print("ACCURACY: ",test_accu)