Open mahenning opened 3 years ago
did you get / solve this issue ?
Unfortunatelly not yet. I tried to run a sequential model with it, but for that it has to be an instance of class Layer. For now I test different possibilities, as the error doesn't give me a starting point for fixing. But if I have a solution for it, I'll post it here.
Regarding the first error
'Cannot convert a symbolic Keras input/output to a numpy array. ' TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.
In the Keras API docs I found this:
Note that even if eager execution is enabled, Input produces a symbolic tensor-like object (i.e. a placeholder). This symbolic tensor-like object can be used with lower-level TensorFlow ops that take tensors as inputs, as such: this is a logistic regression in Keras x = Input(shape=(32,)) y = Dense(16, activation='softmax')(x) model = Model(x, y)
(This behavior does not work for higher-order TensorFlow APIs such as control flow and being directly watched by a tf.GradientTape).
The last line in which the error message is thrown is the ..keras/layers/core.py:1359, which is wrapped backprop.GradientTape.
with backprop.GradientTape(watch_accessed_variables=True) as tape, \ variable_scope.variable_creator_scope(_variable_creator):
We explicitly drop
name
arguments here,# to guard against the case where an op explicitly has a # `name` passed (which is susceptible to producing # multiple ops w/ the same name when the layer is reused) kwargs.pop('name', None) result = self.function(*args, **kwargs)
Does that mean that the haste layers can't be used with an keras Input layer? If so, how can I create a model with them if neither the sequential nor the functional api (requires the input layer AFAIK) can be used? In your example on the main page the layer gets a tensor directly. Do I have to handle batches manually and input the data directly instead of letting the fit function work?
import numpy as np import tensorflow as tf import haste_tf as haste
train_x = np.random.rand(500,40,20) train_y = np.random.rand(500,40,1)
inputs = tf.keras.Input(shape=(train_x.shape[1], train_x.shape[2])) haste1 = haste.LayerNormGRU(20, direction='unidirectional', zoneout=0.1, dropout=0.1) fc1 = tf.keras.layers.Dense(60, activation='relu', kernel_initializer='he_uniform') dr1 = tf.keras.layers.Dropout(0.2) fc2 = tf.keras.layers.Dense(1)
x, state = haste1(inputs, training=True) x = fc1(inputs) x = dr1(x) outputs = fc2(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
print(model.summary())
opt = tf.keras.optimizers.Adam(learning_rate=0.01) model.compile(loss='categorical_crossentropy', optimizer=opt)
model_hist = model.fit(train_x, train_y, epochs=10, batch_size=32, verbose=1)
this code is working for me
For me the error still remains. What python/Tensorflow version did you use for testing?
it's a jupyter notebook on a Docker image:
GPU: "NVIDIA-SMI 418.116.00 Driver Version: 418.116.00 CUDA Version: 10.2"
print(tf.version) 2.3.0 from platform import python_version print(python_version()) 3.7.10 haste.version print(haste.version) 0.5.0-rc0
NAME="Ubuntu" VERSION="18.04.4 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.4 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=bionic UBUNTU_CODENAME=bionic
I have also a version tf 2.4 / py 3.8 but not tested on this image
Does it works for you ?
No. Unfortunately I can't test this in another environment, as haste does not install the libhaste_tf.so when I run pip(3) install haste_tf on neither a second PC (py 3.6) nor my PC with a fresh venv with python 3.8. I tested a haste reinstall in my conda venv with python 3.7 and there the libhaste_tf.so is created (I also checked if it's getting deleted in between). Any idea on that? I understand that this is an unrelated problem to my initial post, so another issue? I really don't get what I'm doing wrong to get all this errors.
I tested it in a google colab (https://colab.research.google.com/drive/1mHuT35cnW5uOgulDjHGPf88gm74O9Caz?usp=sharing) and there the same error occurs.
hello, everyone. actually, the 'real' bug here is this - 'TypeError: haste_lstm() missing 1 required positional argument: 'training'
it happens because keras 'wraps' this call in https://github.com/lmnt-com/haste/blob/459608cfdb4de4d28d2213df8e71f005be8d0f35/frameworks/tf/lstm.py#L211 into TFOpLambda
LIB.haste_lstm expects 'training' argument, but when this call is wrapped, this argument is lost because in constructor of TFOpLambda the flag '_expects_training_arg' explicitly set to False and because of that 'training' is removed
this is what happens in tf 2.5.0
p.s. obvious immediate fix - just comment this line in keras sources
Hey, thank you for figuring that out, I'll try that! But is there hope to get that fixed officially, without changing the keras core file?
import numpy as np import tensorflow as tf import haste_tf as haste
train_x = np.random.rand(500,40,20) train_y = np.random.rand(500,40,1)
inputs = tf.keras.Input(shape=(train_x.shape[1], train_x.shape[2])) haste1 = haste.LayerNormGRU(20, direction='unidirectional', zoneout=0.1, dropout=0.1) fc1 = tf.keras.layers.Dense(60, activation='relu', kernel_initializer='he_uniform') dr1 = tf.keras.layers.Dropout(0.2) fc2 = tf.keras.layers.Dense(1)
x, state = haste1(inputs, training=True) x = fc1(inputs) x = dr1(x) outputs = fc2(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
print(model.summary())
opt = tf.keras.optimizers.Adam(learning_rate=0.01) model.compile(loss='categorical_crossentropy', optimizer=opt)
model_hist = model.fit(train_x, train_y, epochs=10, batch_size=32, verbose=1)
this code is working for me
@thegodone actually, can you add this code as an example of how to mix haste layers into tf.keras model, I think new user will appreciate it.
Hello,
I know this seems more of a debugging problem/problem on my side, but get the following error message when running my code, and it only appears when running it with a haste layer:
I construct the model with the following code:
train_x numpy array shape is (21788, 1000, 4) OS: Ubuntu 20.04 Python version: 3.7 Keras: 2.4.3 Tensorflow: 2.4.1 numpy: 1.19.5 GPU: GTX 1060 CUDA: 11.2
Normally I wouldn't post those error messages on github, but as the code would run without the haste layer, I suspect that the cause of the error lies somewhere close to it, and this repo seems to be the best place to ask and I didn't find any solutions elsewhere. I hope you can help me, I'd really like to try out your implementation for my dataset.