I’m starting to dabble in Tensorflow on my own and want to teach a model a simple linear equation. So to generate training data, I’m writing a little script to produce two arrays, one of 100 tuples of two random integers (x1, x2) and one where each value is y = 2 * x1 + 5 * x2:
import tensorflow as tf
import numpy as np
list_len = 100
x_data_list = []
y_data_list = []
for i in range(list_len):
x1 = np.random.randint(100)
x2 = np.random.randint(100)
x_data_list.append((x1, x2))
y_data_list.append(2 * x1 + 5 * x2)
x_data = np.asarray(x_data_list)
y_data = np.asarray(y_data_list)
Then I want to build a simple model to learn the relationship between these two arrays:
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=2, name="Input_layer", input_shape=(2,), activation="relu"),
tf.keras.layers.Dense(units=16, name="Hidden_layer", activation="relu"),
tf.keras.layers.Dense(units=1, name="Output_layer", activation="relu")
])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(x_data, y_data, epochs=10)
The code runs, but it’s quite useless - it starts with a ridiculously high loss, and that loss never goes down over those epochs.
After some attempts at troubleshooting, my theory is that the problem lies with the dimensionality of the input_shape
parameter in the input layer which I got wrong in such a way that the model doesn’t know it’s supposed to map two input integers from x_data to one output integer from y_data. But my attempts to change that parameter only get me an error that the model expects another dimensionality of input data. And (2,) is what I get when I pass one of the instances of x_data into np.shape()
.
Anyone have an idea what I’m doing wrong? Ideas would be much appreciated!