TypeError: _logger_find_caller() takes from 0 to 1 positional arguments but 2 were given - tensorflow

I'm trying to run a baseline model which can be found here: https://github.com/gniknoil/FG2020-kinship/tree/master/Track1
here is the code:
def baseline_model():
input_1 = Input(shape=(224, 224, 3))
input_2 = Input(shape=(224, 224, 3))
base_model = VGGFace(model='resnet50', include_top=False)
for x in base_model.layers[:-3]:
x.trainable = True
x1 = base_model(input_1)
x2 = base_model(input_2)
x1=GlobalMaxPool2D()(x1)
x2=GlobalAvgPool2D()(x2)
x3 = Subtract()([x1, x2])
x3 = Multiply()([x3, x3])
x1_ = Multiply()([x1, x1])
x2_ = Multiply()([x2, x2])
x4 = Subtract()([x1_, x2_])
x5 = Multiply()([x1, x2])
x = Concatenate(axis=-1)([x3, x4, x5])
# x = Dense(512, activation="relu")(x)
# x = Dropout(0.03)(x)
x = Dense(128, activation="relu")(x)
x = Dropout(0.02)(x)
out = Dense(1, activation="sigmoid")(x)
model = Model([input_1, input_2], out)
model.compile(loss="binary_crossentropy", metrics=['acc'], optimizer=adamv2.Adam(0.00001))
#model.compile(loss=[focal_loss(alpha=.25, gamma=2)], metrics=['acc'], optimizer=Adam(0.00003))
#model.compile(loss=[focal_loss(alpha=.25, gamma=2)], metrics=['acc'], optimizer=Adam(0.00001))
model.summary()
return model
I got the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/1x/xq_p1_3x2wj0drhvr109gkfh0000gn/T/ipykernel_7112/1473799896.py in <module>
----> 1 model = baseline_model()
/var/folders/1x/xq_p1_3x2wj0drhvr109gkfh0000gn/T/ipykernel_7112/726929419.py in baseline_model()
31
32 def baseline_model():
---> 33 input_1 = Input(shape=(224, 224, 3))
34 input_2 = Input(shape=(224, 224, 3))
35
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/engine/input_layer.py in Input(shape, batch_shape, name, dtype, sparse, tensor)
173 if not dtype:
174 dtype = K.floatx()
--> 175 input_layer = InputLayer(batch_input_shape=batch_shape,
176 name=name, dtype=dtype,
177 sparse=sparse,
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/engine/input_layer.py in __init__(self, input_shape, batch_size, batch_input_shape, dtype, input_tensor, sparse, name)
37 if not name:
38 prefix = 'input'
---> 39 name = prefix + '_' + str(K.get_uid(prefix))
40 super(InputLayer, self).__init__(dtype=dtype, name=name)
41
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
72 """
73 global _GRAPH_UID_DICTS
---> 74 graph = tf.get_default_graph()
75 if graph not in _GRAPH_UID_DICTS:
76 _GRAPH_UID_DICTS[graph] = defaultdict(int)
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/tensorflow/python/util/deprecation_wrapper.py in __getattr__(self, name)
115 call_location = _call_location()
116 if not call_location.startswith('<'): # skip locations in Python source
--> 117 logging.warning(
118 'From %s: The name %s is deprecated. Please use %s instead.\n',
119 _call_location(), full_name, rename)
~/opt/anaconda3/envs/env-kinship/lib/python3.8/site-packages/tensorflow/python/platform/tf_logging.py in warning(msg, *args, **kwargs)
164 #tf_export(v1=["logging.error"])
165 def error(msg, *args, **kwargs):
--> 166 get_logger().error(msg, *args, **kwargs)
167
168
~/opt/anaconda3/envs/env-kinship/lib/python3.8/logging/__init__.py in warning(self, msg, *args, **kwargs)
1456 """
1457 if self.isEnabledFor(WARNING):
-> 1458 self._log(WARNING, msg, args, **kwargs)
1459
1460 def warn(self, msg, *args, **kwargs):
~/opt/anaconda3/envs/env-kinship/lib/python3.8/logging/__init__.py in _log(self, level, msg, args, exc_info, extra, stack_info, stacklevel)
1575 #IronPython can use logging.
1576 try:
-> 1577 fn, lno, func, sinfo = self.findCaller(stack_info, stacklevel)
1578 except ValueError: # pragma: no cover
1579 fn, lno, func = "(unknown file)", 0, "(unknown function)"
TypeError: _logger_find_caller() takes from 0 to 1 positional arguments but 2 were given
How can I fix this?
My Keras version is 2.2.4
My tensor flow version is 1.14.0
I'm using python 3.8.1 and an anaconda environment.
And I have tried the solution here: https://medium.com/the-rising-tilde/typeerror-logger-find-caller-takes-from-0-to-1-positional-arguments-but-2-were-given-cb24b74a6125
which doesn't work

You are using Tensorflow 1.14 with python 3.8 which is not compatible as mentioned in this doc.
Please upgrade the Tensorflow version as per installed python version 3.8 in your system which will be Tensorflow >= 2.2 and try again executing the same code.
!pip install --upgrade tensorflow

Related

How to fix "pop from empty list" error while using Keras tuner search method with TPU in google colab?

I previously was able to run the search method of keras tuner on my model with GPU runtime of Google colab. But when I switched to the TPU runtime, I get the following error. I haven't been able to come to the conclusion of how to access a google cloud storage for the TPU runtime to save the checkpoint folder that the keras tuner saves model checkpoints in. I also don't know how to do it and I'm getting the following error. Please help me resolve this issue.
My code:
def post_se(hp):
ip = Input(shape=(6, 128))
x = Masking()(ip)
x = LSTM(units=hp.Choice('lstm_1', values = [8,16,32,64,128,256,512]),return_sequences=True)(x)
x = Dropout(hp.Choice(name='Dropout', values = [0.0,0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]))(x)
x = LSTM(units=hp.Choice('lstm_2', values = [8,16,32,64,128,256,512]))(x)
x = Dropout(hp.Choice(name='Dropout_2', values = [0.0,0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]))(x)
y = Permute((2, 1))(ip)
y = Conv1D(hp.Choice('conv_1_filter', values = [32,64,128,256,512]), hp.Choice(name='conv_1_filter_size', values = [3,5,7,8,9]), padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = squeeze_excite_block(y)
y = Conv1D(hp.Choice('conv_2_filter', values = [32,64,128,256,512]), hp.Choice(name='conv_2_filter_size',values = [3,5,7,8,9]), padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = squeeze_excite_block(y)
y = Conv1D(hp.Choice('conv_3_filter', values = [32,64,128,256,512,]), hp.Choice(name='conv_3_filter_size',values = [3,5,7,8,9]), padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = GlobalAveragePooling1D()(y)
x = concatenate([x,y])
# batch_size = hp.Choice('batch_size', values=[32, 64, 128, 256, 512, 1024, 2048, 4096])
out = Dense(num_classes, activation='softmax')(x)
model = Model(ip, out)
if gpu:
opt = keras.optimizers.Adam(learning_rate=0.001)
if tpu:
opt = keras.optimizers.Adam(learning_rate=8*0.001)
model.compile(optimizer=opt, loss='categorical_crossentropy',metrics=['accuracy'])
# model.summary()
return model
if gpu:
tuner = kt.tuners.BayesianOptimization(post_se,
objective='val_accuracy',
max_trials=30,
seed=42,
project_name='Model_gpu')
# Will stop training if the "val_loss" hasn't improved in 30 epochs.
tuner.search(X_train, train_label, epochs=200, validation_split=0.1, shuffle=True, callbacks=[tensorflow.keras.callbacks.EarlyStopping('val_loss', patience=30)])
if tpu:
print("TPU")
with strategy.scope():
tuner = kt.tuners.BayesianOptimization(post_se,
objective='val_accuracy',
max_trials=30,
seed=42,
project_name='Model_tpu')
# Will stop training if the "val_loss" hasn't improved in 30 epochs.
tuner.search(X_train, train_label, epochs=200, validation_split=0.1, shuffle=True, callbacks=[tensorflow.keras.callbacks.EarlyStopping('val_loss', patience=30)])
The error log
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
/usr/lib/python3.7/contextlib.py in __exit__(self, type, value, traceback)
129 try:
--> 130 self.gen.throw(type, value, traceback)
131 except StopIteration as exc:
10 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in resource_creator_scope(resource_type, resource_creator)
2957 resource_creator):
-> 2958 yield
2959
<ipython-input-15-24c1e1bb603d> in <module>()
17 # Will stop training if the "val_loss" hasn't improved in 30 epochs.
---> 18 tuner.search(X_train, train_label, epochs=200, validation_split=0.1, shuffle=True, callbacks=[tensorflow.keras.callbacks.EarlyStopping('val_loss', patience=30)])
/usr/local/lib/python3.7/dist-packages/keras_tuner/engine/base_tuner.py in search(self, *fit_args, **fit_kwargs)
178 self.on_trial_begin(trial)
--> 179 results = self.run_trial(trial, *fit_args, **fit_kwargs)
180 # `results` is None indicates user updated oracle in `run_trial()`.
/usr/local/lib/python3.7/dist-packages/keras_tuner/engine/tuner.py in run_trial(self, trial, *args, **kwargs)
303 copied_kwargs["callbacks"] = callbacks
--> 304 obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs)
305
/usr/local/lib/python3.7/dist-packages/keras_tuner/engine/tuner.py in _build_and_fit_model(self, trial, *args, **kwargs)
233 model = self._try_build(hp)
--> 234 return self.hypermodel.fit(hp, model, *args, **kwargs)
235
/usr/local/lib/python3.7/dist-packages/keras_tuner/engine/hypermodel.py in fit(self, hp, model, *args, **kwargs)
136 """
--> 137 return model.fit(*args, **kwargs)
138
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in _numpy(self)
1116 except core._NotOkStatusException as e: # pylint: disable=protected-access
-> 1117 raise core._status_to_exception(e) from None # pylint: disable=protected-access
1118
UnimplementedError: File system scheme '[local]' not implemented (file: './untitled_project/trial_78ed6883514d67dc6222064095c134cb/checkpoints/epoch_0/checkpoint_temp/part-00000-of-00001')
Encountered when executing an operation using EagerExecutor. This error cancels all future operations and poisons their output tensors.
During handling of the above exception, another exception occurred:
IndexError Traceback (most recent call last)
<ipython-input-15-24c1e1bb603d> in <module>()
16 seed=42)
17 # Will stop training if the "val_loss" hasn't improved in 30 epochs.
---> 18 tuner.search(X_train, train_label, epochs=200, validation_split=0.1, shuffle=True, callbacks=[tensorflow.keras.callbacks.EarlyStopping('val_loss', patience=30)])
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py in __exit__(self, exception_type, exception_value, traceback)
454 "tf.distribute.set_strategy() out of `with` scope."),
455 e)
--> 456 _pop_per_thread_mode()
457
458
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribution_strategy_context.py in _pop_per_thread_mode()
64
65 def _pop_per_thread_mode():
---> 66 ops.get_default_graph()._distribution_strategy_stack.pop(-1) # pylint: disable=protected-access
67
68
IndexError: pop from empty list
For some extra info, I am attaching my code in this post.
This is your error:
UnimplementedError: File system scheme '[local]' not implemented (file: './untitled_project/trial_78ed6883514d67dc6222064095c134cb/checkpoints/epoch_0/checkpoint_temp/part-00000-of-00001')
See https://stackoverflow.com/a/62881833/14043558 for a solution.

How to set Keras layer to include `None` in return tuple

I am trying to make a Keras layer that returns None in its tuple.
class transformer_IO(tf.keras.layers.Layer):
def call(self, input):
return (input, None, None, None)
However, when I try to compile with this error, I get
AttributeError: 'NoneType' object has no attribute 'shape'
Here is an example
!pip install transformers
from transformers import TFBertModel
import tensorflow as tf
from copy import deepcopy
class transformer_IO(tf.keras.layers.Layer):
def call(self, input):
return (input, None, None, None)
def get_functional_model_protoFix():
bioRoberta_f = TFBertModel.from_pretrained('bert-base-uncased', from_pt=True)
Q_Tlayer0_f = deepcopy(bioRoberta_f.layers[0].encoder.layer[8])
Q_Tlayer0_f._name = Q_Tlayer0_f._name + 'Query_f'
Q_Tlayer1_f = deepcopy(bioRoberta_f.layers[0].encoder.layer[9])
Q_Tlayer1_f._name = Q_Tlayer1_f._name + 'Query_f'
transIO = transformer_IO()
inputIds = tf.keras.Input(shape=(None,), dtype=tf.int32, name='input_Q')
Q_outputs = bioRoberta_f(inputIds)[0]
Q_outputs = transIO(Q_outputs)
Q_outputs = Q_Tlayer0_f(Q_outputs)[0]
Q_outputs = transIO(Q_outputs)
Q_outputs = Q_Tlayer1_f(Q_outputs)[0]
modelNew = tf.keras.Model(inputs=inputIds, outputs=Q_outputs)
return modelNew
model_functional = get_functional_model_protoFix()
model_functional.compile(loss=loss_fn,
optimizer=tfa.optimizers.AdamW(weight_decay=1e-4, learning_rate=1e-5,
epsilon=1e-06))
Full error message
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-35-a029c18cecf9> in <module>()
----> 1 model_functional_new = get_functional_model_protoFix()
2 model_functional_new.compile(loss=loss_fn,
3 optimizer=tfa.optimizers.AdamW(weight_decay=1e-4, learning_rate=1e-5,
4 epsilon=1e-06))
7 frames
<ipython-input-34-693ee085f848> in get_functional_model_protoFix()
13
14 Q_outputs = bioRoberta_f(inputIds)[0]
---> 15 Q_outputs = transIO(Q_outputs)
16 Q_outputs = Q_Tlayer0_f(Q_outputs)[0]
17 Q_outputs = transIO(Q_outputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
952 kwargs.pop('mask')
953 inputs, outputs = self._set_connectivity_metadata_(
--> 954 inputs, outputs, args, kwargs)
955 self._handle_activity_regularization(inputs, outputs)
956 self._set_mask_metadata(inputs, outputs, input_masks)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
2312 # This updates the layer history of the output tensor(s).
2313 self._add_inbound_node(
-> 2314 input_tensors=inputs, output_tensors=outputs, arguments=arguments)
2315 return inputs, outputs
2316
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments)
2342 input_tensors=input_tensors,
2343 output_tensors=output_tensors,
-> 2344 arguments=arguments)
2345
2346 # Update tensor history metadata.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/node.py in __init__(self, outbound_layer, inbound_layers, node_indices, tensor_indices, input_tensors, output_tensors, arguments)
108 self.input_shapes = nest.map_structure(backend.int_shape, input_tensors)
109 # Nested structure of shape tuples, shapes of output_tensors.
--> 110 self.output_shapes = nest.map_structure(backend.int_shape, output_tensors)
111
112 # Optional keyword arguments to layer's `call`.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
615
616 return pack_sequence_as(
--> 617 structure[0], [func(*x) for x in entries],
618 expand_composites=expand_composites)
619
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
615
616 return pack_sequence_as(
--> 617 structure[0], [func(*x) for x in entries],
618 expand_composites=expand_composites)
619
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in int_shape(x)
1201 """
1202 try:
-> 1203 shape = x.shape
1204 if not isinstance(shape, tuple):
1205 shape = tuple(shape.as_list())
AttributeError: 'NoneType' object has no attribute 'shape'

Using tf.data.Dataset with tf Hub Modules

How do I feed a tf.keras model, that includes a 1D input TF Hub module, with a tf.data.Dataset?
(Ultimately, the aim is to use a single tf.data.Dataset with a multi-input, multi-output keras funtional api model.)
Tried this:
import tensorflow as tf
import tensorflow_hub as hub
embed = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
hub_layer = hub.KerasLayer(embed, output_shape=[20], input_shape=[],
dtype=tf.string, trainable=True, name='hub_layer')
# From tf hub webpage: "The module takes a batch of sentences in a 1-D tensor of strings as input."
input_tensor = tf.keras.Input(shape=(), dtype=tf.string)
hub_tensor = hub_layer(input_tensor)
x = tf.keras.layers.Dense(16, activation='relu')(hub_tensor)#(x)
main_output = tf.keras.layers.Dense(units=4, activation='softmax', name='main_output')(x)
model = tf.keras.models.Model(inputs=[input_tensor], outputs=[main_output])
# This works as expected.
X_tensor = tf.constant(['Hello World', 'The Quick Brown Fox'])
model(X_tensor)
# This fails
X_ds = tf.data.Dataset.from_tensors(X_tensor)
X_ds.element_spec
model(X_ds)
Expectation was that the 1D tensor in the dataset would be automatically extracted and consumed by the model.
Error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
21 X_ds = tf.data.Dataset.from_tensors(X_tensor)
22 X_ds.element_spec
---> 23 model(X_ds)
24
25
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
966 with base_layer_utils.autocast_context_manager(
967 self._compute_dtype):
--> 968 outputs = self.call(cast_inputs, *args, **kwargs)
969 self._handle_activity_regularization(inputs, outputs)
970 self._set_mask_metadata(inputs, outputs, input_masks)
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in call(self, inputs, training, mask)
717 return self._run_internal_graph(
718 inputs, training=training, mask=mask,
--> 719 convert_kwargs_to_constants=base_layer_utils.call_context().saving)
720
721 def compute_output_shape(self, input_shape):
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in _run_internal_graph(self, inputs, training, mask, convert_kwargs_to_constants)
835 tensor_dict = {}
836 for x, y in zip(self.inputs, inputs):
--> 837 y = self._conform_to_reference_input(y, ref_input=x)
838 x_id = str(id(x))
839 tensor_dict[x_id] = [y] * self._tensor_usage_count[x_id]
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in _conform_to_reference_input(self, tensor, ref_input)
959 # Dtype handling.
960 if isinstance(ref_input, (ops.Tensor, composite_tensor.CompositeTensor)):
--> 961 tensor = math_ops.cast(tensor, dtype=ref_input.dtype)
962
963 return tensor
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
178 """Call target, and fall back on dispatchers if there is a TypeError."""
179 try:
--> 180 return target(*args, **kwargs)
181 except (TypeError, ValueError):
182 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in cast(x, dtype, name)
785 # allows some conversions that cast() can't do, e.g. casting numbers to
786 # strings.
--> 787 x = ops.convert_to_tensor(x, name="x")
788 if x.dtype.base_dtype != base_type:
789 x = gen_math_ops.cast(x, base_type, name=name)
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1339
1340 if ret is None:
-> 1341 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1342
1343 if ret is NotImplemented:
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
319 as_ref=False):
320 _ = as_ref
--> 321 return constant(v, dtype=dtype, name=name)
322
323
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
260 """
261 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 262 allow_broadcast=True)
263
264
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
268 ctx = context.context()
269 if ctx.executing_eagerly():
--> 270 t = convert_to_eager_tensor(value, ctx, dtype)
271 if shape is None:
272 return t
~/projects/email_analysis/email_venv/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
94 dtype = dtypes.as_dtype(dtype).as_datatype_enum
95 ctx.ensure_initialized()
---> 96 return ops.EagerTensor(value, ctx.device_name, dtype)
97
98
ValueError: Attempt to convert a value () with an unsupported type () to a Tensor.
The point of a dataset is to provide a sequence of tensors, like here:
all_data = tf.constant([['Hello', 'World'], ['Brown Fox', 'lazy dog']])
ds = tf.data.Dataset.from_tensor_slices(all_data)
for tensor in ds:
print(tensor)
which outputs
tf.Tensor([b'Hello' b'World'], shape=(2,), dtype=string)
tf.Tensor([b'Brown Fox' b'lazy dog'], shape=(2,), dtype=string)
Instead of just printing tensor, you can compute with it:
for tensor in ds:
print(hub_layer(tensor))
which outputs 2 tensors of shape (2,20) each.
For more, see https://www.tensorflow.org/guide/data.

TypeError: argument 0 is not a Variable

I am trying to learn CNN with my own data. The shape of the data is (1224, 15, 23). 1224 is the number of data, and each data is (15, 23). CNN is built with PyTorch.
I think there is no logical error because conv2D needs 4-D tensor, and I feed (batch, channel, x, y).
when I build an instance of the Net class I got this error.
TypeError: argument 0 is not a Variable
I have been using PyTroch for half of a year but this error is the first time and I am still confused.
Here is my code.
class Net(nn.Module):
def __init__(self, n):
super(Net,self).__init__()
self.conv = nn.Sequential(nn.Conv2d(1, 32, kernel_size=3, stride=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1), # 64 x 9 x 17
nn.ReLU()
)
conv_out_size = self._get_conv_out(input_shape)
self.fc = nn.Sequential(nn.Linear(64 * 9 * 17, 128),
nn.ReLU(),
nn.Linear(128, n)
)
def _get_conv_out(self, shape):
o = self.conv(torch.zeros(1, *shape))
return int(np.prod(o.size()))
def forward(self, x):
conv_out = self.conv(x).view(x.size()[0], -1)
return sefl.fc(conv_out)
if __name__=='__main__':
num_epochs = 1
num_classes = 2
input_shape = train_img[0].shape # 1, 15, 23
net = Net(num_classes)
iteration = 51
BATCH_SIZE = 24
LEARNING_RATE = 0.0001
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE)
loss_list= []
batch_index = 0
# train
for epoch in range(num_epochs):
for i in range(iteration):
input_img = torch.FloatTensor(train_img[batch_index: batch_index + BATCH_SIZE])
print(input_img.size()) # 24, 1, 15, 23
outputs = net(input_img)
loss = criterion(outputs, labels)
loss_list.append(loss.item())
# Backprop
opimizer.zero_grad()
loss.backward()
optimizer.step()
And the error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-179-0f6bc7588c29> in <module>
4 input_shape = train_img[0].shape # 1, 15, 23
5
----> 6 net = Net(num_classes)
7 iteration = 51
8 BATCH_SIZE = 24
<ipython-input-178-8a68d4a0dc4a> in __init__(self, n)
11 )
12
---> 13 conv_out_size = self._get_conv_out(input_shape)
14 self.fc = nn.Sequential(nn.Linear(64 * 9 * 17, 128),
15 nn.ReLU(),
<ipython-input-178-8a68d4a0dc4a> in _get_conv_out(self, shape)
18
19 def _get_conv_out(self, shape):
---> 20 o = self.conv(torch.zeros(1, *shape))
21 return int(np.prod(o.size()))
22
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
355 result = self._slow_forward(*input, **kwargs)
356 else:
--> 357 result = self.forward(*input, **kwargs)
358 for hook in self._forward_hooks.values():
359 hook_result = hook(self, input, result)
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
65 def forward(self, input):
66 for module in self._modules.values():
---> 67 input = module(input)
68 return input
69
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
355 result = self._slow_forward(*input, **kwargs)
356 else:
--> 357 result = self.forward(*input, **kwargs)
358 for hook in self._forward_hooks.values():
359 hook_result = hook(self, input, result)
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
280 def forward(self, input):
281 return F.conv2d(input, self.weight, self.bias, self.stride,
--> 282 self.padding, self.dilation, self.groups)
283
284
C:\DTools\Anaconda3\envs\tensorflow\lib\site-packages\torch\nn\functional.py in conv2d(input, weight, bias, stride, padding, dilation, groups)
88 _pair(0), groups, torch.backends.cudnn.benchmark,
89 torch.backends.cudnn.deterministic, torch.backends.cudnn.enabled)
---> 90 return f(input, weight, bias)
91
92
TypeError: argument 0 is not a Variable
Your code actually works for PyTorch >= 0.4.1. I guess your PyTorch version is < 0.4 and so you need to pass a Variable in the following line.
o = conv(torch.autograd.Variable(torch.zeros(1, *x.shape)))
In PyTorch >= 0.4.1, the concept of Variable no longer exists. So, torch.FloatTensor can be directly passed to NN layers.

Input tensors to a Model must come from `tf.layers.Input` when I concatenate two models with Keras API on Tensorflow

I'm creating a wide and deep model using Keras functional API on tensorflow.
When I try to merge the two models, the below error occurred.
--------------------------------------------------------------------------- ValueError Traceback (most recent call
last) in ()
1 merged_out = tf.keras.layers.concatenate([wide_model.output, deep_model.output])
2 merged_out = tf.keras.layers.Dense(1)(merged_out)
----> 3 combined_model = tf.keras.Model(inputs=wide_model.input + [deep_model.input], outputs=merged_out)
4 print(combined_model.summary())
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py
in init(self, *args, **kwargs)
111
112 def init(self, *args, **kwargs):
--> 113 super(Model, self).init(*args, **kwargs)
114 # Create a cache for iterator get_next op.
115 self._iterator_get_next = weakref.WeakKeyDictionary()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py
in init(self, *args, **kwargs)
77 'inputs' in kwargs and 'outputs' in kwargs):
78 # Graph network
---> 79 self._init_graph_network(*args, **kwargs)
80 else:
81 # Subclassed network
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/checkpointable/base.py
in _method_wrapper(self, *args, **kwargs)
362 self._setattr_tracking = False # pylint: disable=protected-access
363 try:
--> 364 method(self, *args, **kwargs)
365 finally:
366 self._setattr_tracking = previous_value # pylint: disable=protected-access
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py
in _init_graph_network(self, inputs, outputs, name)
193 'must come from tf.layers.Input. '
194 'Received: ' + str(x) +
--> 195 ' (missing previous layer metadata).')
196 # Check that x is an input tensor.
197 # pylint: disable=protected-access
ValueError: Input tensors to a Model must come from tf.layers.Input.
Received: Tensor("add_1:0", shape=(1, ?, 163), dtype=float32) (missing
previous layer metadata).
Here is the code for concatenating the two.
merged_out = tf.keras.layers.concatenate([wide_model.output, deep_model.output])
merged_out = tf.keras.layers.Dense(1)(merged_out)
combined_model = tf.keras.Model(inputs=wide_model.input + [deep_model.input], outputs=merged_out)
print(combined_model.summary())
For each model's inputs, I tried using tf.layers.Inputwith
inputs = tf.placeholder(tf.float32, shape=(None,X_resampled.shape[1]))
deep_inputs = tf.keras.Input(tensor=(inputs))
to make them tf.layers.Input as this page mentions.
But I'm still facing the same issue.
I'm using tensorflow==1.10.0
Could someone help me solving this issue?
Thanks!
In inputs=wide_model.input + [deep_model.input], wide.model.input is probably not a list, so that you are passing a new Add tensor instead of a list of inputs. Try passing inputs=[wide_model.input] + [deep_model.input] instead