Search code examples
pythontensorflowkeraslayer

how to configure the layouts for like this input and output example in tensorFlow and keras


I have those input and output, and i want to configure the layouts using TensorFlow and Keras:

input = [[5, 3, 10], [2, 1, 2], [6,2,9], [1,1,0], [10, 4, 3], [3, 5, 6], [8, 1, 10], [4, 4, 3],[7, 3, 6], [4, 2, 12]] # 
output = [2000, 500, 2100, 300, 3000, 1200, 3400, 1300, 2500, 1900] 

I have tried this but doesn't work:

model = tf.keras.Sequential([
          tf.keras.layers.Dense(1, input_shape=(1,10))
])
model.compile(loss='mean_squared_error', optimizer= tf.keras.optimizers.Adam(learning_rate=0.1))
model.fit(input, output, epochs=800, verbose=False)
predict = model.predict([20,6,2])
print(predict)

Solution

  • Try changing your input shape to (3,), since each samples has 3 features and when making predictions, add an additional dimension for the batch size:

    import tensorflow as tf
    
    input = [[5, 3, 10], [2, 1, 2], [6,2,9], [1,1,0], [10, 4, 3], [3, 5, 6], [8, 1, 10], [4, 4, 3],[7, 3, 6], [4, 2, 12]] # 
    output = [2000, 500, 2100, 300, 3000, 1200, 3400, 1300, 2500, 1900]
    
    model = tf.keras.Sequential([
              tf.keras.layers.Dense(1, input_shape=(3, ))
    ])
    model.compile(loss='mean_squared_error', optimizer= tf.keras.optimizers.Adam(learning_rate=0.1))
    model.fit(input, output, epochs=800, verbose=False)
    predict = model.predict([[20,6,2]])
    print(predict)
    
    [[1993.9138]]