New to tensorflow (and ai in general) - I am trying to build the simplest feed-forward model using tensorflow. I would like to run a sequential model on the fashion mnist data. The only complication I am trying to perform is having some kind of middle layer that adds the former layer with the inputs.
I tried the following:
import tensorflow as tf
import numpy as np
fashion_mnist = tf.keras.datasets.fashion_mnist
(x_train, y_train) , (x_test, y_test) = fashion_mnist.load_data()
x_train, x_test = x_train/255.0, x_test/255.0
input_layer = tf.keras.layers.Flatten(input_shape=(28,28))
inner_layer = tf.keras.layers.Dense(28*28, activation="relu")
hidden_layer_01 = tf.keras.layers.Dense(16, activation="relu")
hidden_layer_02 = tf.keras.layers.Dense(16, activation="relu")
hidden_layer_03 = tf.keras.layers.Dense(16, activation="relu")
hidden_layer_04 = tf.keras.layers.Dense(16, activation="relu")
hidden_layer_add = tf.keras.layers.add(28*28)([input_layer, inner_layer])
hidden_layer_dropout = tf.keras.Dropout(0.1)
output_layer = tf.keras.layers.Dense(10)
model = tf.keras.Model(
input_layer,
hidden_layer_01,
hidden_layer_02,
inner_layer,
hidden_layer_add,
hidden_layer_03,
hidden_layer_04,
hidden_layer_dropout,
output_layer
)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10)
print("EVALUATING...")
model.evaluate(x_test, y_test, verbose=2)
I get:
Traceback (most recent call last): File "D:\PROJECTS\Python\pythonProject\fashionMnist01\main.py", line 30, in hidden_layer_add = tf.keras.layers.add(28*28)([input_layer, inner_layer]) File "D:\PROJECTS\Python\pythonProject\fashionMnist01\venv\lib\site-packages\keras\layers\merging\add.py", line 92, in add return Add(**kwargs)(inputs) File "D:\PROJECTS\Python\pythonProject\fashionMnist01\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "D:\PROJECTS\Python\pythonProject\fashionMnist01\venv\lib\site-packages\keras\layers\merging\base_merge.py", line 84, in build if not isinstance(input_shape[0], tuple): IndexError: tuple index out of range
How can I make this work?