I tried to run it on my local installation (PyCharm) and ran headlong into PyCharm bug PY-53599: tensorflow.keras subpackages are unresolved in Tensorflow >= 2.6.0. There goes the whole morning.
Still, I have found the following:
- One should not use
keras.Input
but keras.layers.InputLayer
- Using
keras.layers.InputLayer
in the sequential model is not necessary.
For point 1, we get this warning:
WARNING:tensorflow:Please add `keras.layers.InputLayer` instead of
`keras.Input` to Sequential model.
`keras.Input` is intended to be used by Functional model.
So:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import InputLayer
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.optimizers import Adam
import sys
print(f"We are using Python version {sys.version} on {sys.platform}")
print(f"We are using tensorflow version {tf.__version__}")
model2 = Sequential([
InputLayer(shape=(2,)),
Dense(units=3, activation="relu", name="layer1"),
Dense(units=1, activation="linear", name="layer2")
])
model2.compile(
loss=BinaryCrossentropy(from_logits=True),
optimizer=Adam(learning_rate=0.01)
)
If you are wrestling with PyCharm, the code needs to be modified and one is absolutely not supposed to do the imports like this:
import tensorflow as tf
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.layers import InputLayer
from tensorflow.python.keras.losses import BinaryCrossentropy
from tensorflow.python.keras.optimizer_v2.adam import Adam
import sys
print(f"We are using Python version {sys.version} on {sys.platform}")
print(f"We are using tensorflow version {tf.__version__}")
model2 = Sequential([
InputLayer(input_shape=(2,)),
Dense(units=3, activation="relu", name="layer1"),
Dense(units=1, activation="linear", name="layer2")
])
model2.compile(
loss=BinaryCrossentropy(from_logits=True),
optimizer=Adam(learning_rate=0.01)
)
Additionally, for some Lovecraftian reason, InputLayer
does not recognize the shape
named argument now but demands input_shape
. This is bad, as this means some older version is being used.
Also, the documentation: