CS5720 - Week 9
Slide 170 of 180

Interactive Framework Demo

TensorFlow
tensorflow_model.py
# TensorFlow/Keras approach import tensorflow as tf # Build model model = tf.keras.Sequential([ tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ]) # Compile and train model.compile(optimizer='adam', loss='sparse_categorical_crossentropy') model.fit(X_train, y_train, epochs=10)
PyTorch
pytorch_model.py
# PyTorch approach import torch import torch.nn as nn # Define model class class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 64) self.fc2 = nn.Linear(64, 32) self.fc3 = nn.Linear(32, 10) def forward(self, x): x = torch.relu(self.fc1(x)) x = torch.relu(self.fc2(x)) return self.fc3(x) model = Net()
Prepared by Dr. Gorkem Kar