ML with multiple input arrays in python
Question:
I would like to make a neural network which has several inputs and 3 outputs but when I try to put 2 entries in my program it does not want.
import keras
import numpy as np
import tensorflow as tf
model = keras.Sequential(
[
tf.keras.layers.Dense(units=5, input_shape=[2]),
tf.keras.layers.Dense(units=1, input_shape=[5]),
]
)
model.compile('sgd', loss='mean_squared_error')
# Call model on a test input
x = np.array([0.0,1.0,2.0,3.0,4.0,5.0,6.0])
x2 = np.array([2.0,3.0,4.0,5.0,6.0,7.0,8.0])
y = np.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0])
model.fit(x=[x,x2], y=y, epochs=3000)
print(model.predict([10.0]))
And I have this as an error I have already been looked at but nothing related to what I want to do:
2022-08-19 15:57:56.968978: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-08-19 15:57:57.249294: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 5501 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060 Ti, pci bus id: 0000:05:00.0, compute capability: 8.6
Epoch 1/3000
Traceback (most recent call last):
File "C:UsersgoranPycharmProjectsAI_cryptomain.py", line 18, in <module>
model.fit(x=[x,x2], y=y, epochs=3000)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonframeworkfunc_graph.py", line 1147, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 1021, in train_function *
return step_function(self, iterator)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 1000, in run_step **
outputs = model.train_step(data)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 859, in train_step
y_pred = self(x, training=True)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasengineinput_spec.py", line 200, in assert_input_compatibility
raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
ValueError: Layer "sequential" expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None,) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=float32>]
Process finished with exit code 1
Answers:
I find that using Keras Functional API is way more flexible than a sequential model.
Here an example on how you can create a model with multiple inputs:
from tensorflow.keras.models import Model
from tensorflow import keras
from tensorflow.keras.layers import Dense, Input
from keras.utils.vis_utils import plot_model
import numpy as np
input_1 = Input(shape=(1,))
input_2 = Input(shape=(1,))
x = keras.layers.concatenate([input_1, input_2])
x = (Dense(10))(x)
model = Model(inputs = ([input_1, input_2]), outputs = x)
model.compile(loss="mean_squared_error", optimizer='sgd')
x = np.array([0.0,1.0,2.0,3.0,4.0,5.0,6.0])
x2 = np.array([2.0,3.0,4.0,5.0,6.0,7.0,8.0])
y = np.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0])
model.fit(x=[x,x2], y=y, epochs=3)
plot_model(model, show_shapes=True, show_layer_names=True)
The model will have the following architecture:
I would like to make a neural network which has several inputs and 3 outputs but when I try to put 2 entries in my program it does not want.
import keras
import numpy as np
import tensorflow as tf
model = keras.Sequential(
[
tf.keras.layers.Dense(units=5, input_shape=[2]),
tf.keras.layers.Dense(units=1, input_shape=[5]),
]
)
model.compile('sgd', loss='mean_squared_error')
# Call model on a test input
x = np.array([0.0,1.0,2.0,3.0,4.0,5.0,6.0])
x2 = np.array([2.0,3.0,4.0,5.0,6.0,7.0,8.0])
y = np.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0])
model.fit(x=[x,x2], y=y, epochs=3000)
print(model.predict([10.0]))
And I have this as an error I have already been looked at but nothing related to what I want to do:
2022-08-19 15:57:56.968978: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-08-19 15:57:57.249294: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 5501 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060 Ti, pci bus id: 0000:05:00.0, compute capability: 8.6
Epoch 1/3000
Traceback (most recent call last):
File "C:UsersgoranPycharmProjectsAI_cryptomain.py", line 18, in <module>
model.fit(x=[x,x2], y=y, epochs=3000)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packagestensorflowpythonframeworkfunc_graph.py", line 1147, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 1021, in train_function *
return step_function(self, iterator)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 1000, in run_step **
outputs = model.train_step(data)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasenginetraining.py", line 859, in train_step
y_pred = self(x, training=True)
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersgoranAppDataLocalProgramsPythonPython39libsite-packageskerasengineinput_spec.py", line 200, in assert_input_compatibility
raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
ValueError: Layer "sequential" expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None,) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=float32>]
Process finished with exit code 1
I find that using Keras Functional API is way more flexible than a sequential model.
Here an example on how you can create a model with multiple inputs:
from tensorflow.keras.models import Model
from tensorflow import keras
from tensorflow.keras.layers import Dense, Input
from keras.utils.vis_utils import plot_model
import numpy as np
input_1 = Input(shape=(1,))
input_2 = Input(shape=(1,))
x = keras.layers.concatenate([input_1, input_2])
x = (Dense(10))(x)
model = Model(inputs = ([input_1, input_2]), outputs = x)
model.compile(loss="mean_squared_error", optimizer='sgd')
x = np.array([0.0,1.0,2.0,3.0,4.0,5.0,6.0])
x2 = np.array([2.0,3.0,4.0,5.0,6.0,7.0,8.0])
y = np.array([1.0,2.0,3.0,4.0,5.0,6.0,7.0])
model.fit(x=[x,x2], y=y, epochs=3)
plot_model(model, show_shapes=True, show_layer_names=True)
The model will have the following architecture: