0% found this document useful (0 votes)
16 views4 pages

Cifaar 10

This document shows code for building and training a neural network model for digit recognition on the MNIST dataset using Keras. It imports necessary libraries, preprocesses the MNIST data, defines and compiles a sequential model with multiple dense and dropout layers, trains the model for 20 epochs, and plots the loss curves. Random images from the test set are also displayed to check the model's predictions.

Uploaded by

Omar Omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Cifaar 10

This document shows code for building and training a neural network model for digit recognition on the MNIST dataset using Keras. It imports necessary libraries, preprocesses the MNIST data, defines and compiles a sequential model with multiple dense and dropout layers, trains the model for 20 epochs, and plots the loss curves. Random images from the test set are also displayed to check the model's predictions.

Uploaded by

Omar Omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#!

/usr/bin/env python
# coding: utf-8

# In[1]:

import numpy as np
import math

# In[2]:

def sigmoid(x):
return 1 / (1 + math.exp(-x))

# In[3]:

sigmoid(0.55*0.5+(-0.35)*0.45+0.15)

# In[4]:

from tensorflow.keras import Sequential


from tensorflow.keras.layers import Dense

# In[5]:

model = Sequential()
model.add(Dense(4, activation='relu', input_shape=(3,)))
model.add(Dense(4, activation='relu'))
model.add(Dense(1))

# In[18]:

4*3+4+4*4+4+4*1+1

# In[19]:

model.summary()

# In[20]:

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop, Adam
import matplotlib.pyplot as plt

# In[21]:

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# In[22]:

randindices=np.random.randint(x_train.shape[0],size=3)
plt.figure()
plt.subplot(1,3,1)
plt.imshow(x_train[randindices[0],:,:])
plt.subplot(1,3,2)
plt.imshow(x_train[randindices[1],:,:])
plt.subplot(1,3,3)
plt.imshow(x_train[randindices[2],:,:])

# In[23]:

print(y_train[randindices[0]],y_train[randindices[1]],y_train[randindices
[2]])

# In[24]:

x_train[randindices[0],:,:]

# In[32]:

x_train = x_train.reshape(60000, 784)


x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# In[33]:

num_classes = 10

y_train = keras.utils.to_categorical(y_train, num_classes)


y_test = keras.utils.to_categorical(y_test, num_classes)
# In[34]:

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.3))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

# In[35]:

model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=1e-3),
metrics=['accuracy'])

# In[36]:

batch_size = 128
epochs = 20

# In[30]:

history = model.fit(x_train, y_train,


batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))

# In[37]:

plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='val')
plt.legend()

# In[42]:

randindices=np.random.randint(x_test.shape[0],size=3)
plt.figure()
plt.subplot(1,3,1)
plt.imshow(np.reshape(x_test[randindices[0],:],(28,28)), cmap='gray')
plt.subplot(1,3,2)
plt.imshow(np.reshape(x_test[randindices[1],:],(28,28)), cmap='gray')
plt.subplot(1,3,3)
plt.imshow(np.reshape(x_test[randindices[2],:],(28,28)), cmap='gray')

# In[ ]:

# In[ ]:

You might also like