Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Generative AI for Intelligent Data Handling/Lab 3 RNN implementation.ipynb
3074 views
Kernel: Python 3 (ipykernel)

Implement the MNIST fashion dataset using a Recurrent Neural Network (RNN) in R,

you can follow these steps:

  • Load the MNIST fashion dataset: The MNIST fashion dataset can be easily accessed through Keras in R.

  • Preprocess the data: Preprocess the data by normalizing it and reshaping it if needed.

  • Build the RNN model: Construct a recurrent neural network model using packages like TensorFlow or Keras.

  • Compile the model: Compile the model with appropriate loss function, optimizer, and metrics.

  • Train the model: Fit the model on the training data.

  • Evaluate the model: Evaluate the performance of the model on the test data

import tensorflow as tf from tensorflow.keras.datasets import fashion_mnist from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense, Embedding from tensorflow.keras.utils import to_categorical # Load MNIST fashion dataset (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() # Preprocess the data x_train = x_train / 255.0 x_test = x_test / 255.0 y_train = to_categorical(y_train) y_test = to_categorical(y_test) # Define RNN model model = Sequential([ Embedding(input_dim=784, output_dim=128), LSTM(units=128), Dense(units=10, activation='softmax') ]) # Compile the model model.compile( loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] ) # Train the model history = model.fit( x_train, y_train, epochs=10, batch_size=128, validation_split=0.2 ) # Evaluate the model test_loss, test_accuracy = model.evaluate(x_test, y_test) print(f'Test Loss: {test_loss}, Test Accuracy: {test_accuracy}')
Epoch 1/10 WARNING:tensorflow:Model was constructed with shape (None, None) for input KerasTensor(type_spec=TensorSpec(shape=(None, None), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (128, 28, 28).
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [2], in <cell line: 31>() 24 model.compile( 25 loss='categorical_crossentropy', 26 optimizer='adam', 27 metrics=['accuracy'] 28 ) 30 # Train the model ---> 31 history = model.fit( 32 x_train, y_train, 33 epochs=10, 34 batch_size=128, 35 validation_split=0.2 36 ) 38 # Evaluate the model 39 test_loss, test_accuracy = model.evaluate(x_test, y_test)
File ~\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs) 65 except Exception as e: # pylint: disable=broad-except 66 filtered_tb = _process_traceback_frames(e.__traceback__) ---> 67 raise e.with_traceback(filtered_tb) from None 68 finally: 69 del filtered_tb
File C:NaN, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator) 13 try: 14 do_return = True ---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) 16 except: 17 do_return = False
ValueError: in user code: File "C:\Users\suyashi144893\Anaconda3\lib\site-packages\keras\engine\training.py", line 1051, in train_function * return step_function(self, iterator) File "C:\Users\suyashi144893\Anaconda3\lib\site-packages\keras\engine\training.py", line 1040, in step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) File "C:\Users\suyashi144893\Anaconda3\lib\site-packages\keras\engine\training.py", line 1030, in run_step ** outputs = model.train_step(data) File "C:\Users\suyashi144893\Anaconda3\lib\site-packages\keras\engine\training.py", line 889, in train_step y_pred = self(x, training=True) File "C:\Users\suyashi144893\Anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "C:\Users\suyashi144893\Anaconda3\lib\site-packages\keras\engine\input_spec.py", line 214, in assert_input_compatibility raise ValueError(f'Input {input_index} of layer "{layer_name}" ' ValueError: Exception encountered when calling layer "sequential" (type Sequential). Input 0 of layer "lstm" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (128, 28, 28, 128) Call arguments received by layer "sequential" (type Sequential): • inputs=tf.Tensor(shape=(128, 28, 28), dtype=float32) • training=True • mask=None