Simple LSTM model : No attr named '_XlaCompile' in name error - python

I am very new to machine learning and I came across an error while attempting to make a simple LSTM model, and I am absolutely clueless how to debug this. I am using Keras version 2.2.2.
My code looks more or less like this:
model = Sequential()
model.add(Embedding(400001, emb_dim, trainable=False, input_length = 56, weights = [emb_matrix]))
model.add(LSTM(128, return_sequences=False))
model.add(Dense(5, activation='softmax'))
model.summary()
model.fit(train_in, train_out, epochs = 50, batch_size = 32, shuffle=True)
My inputs were initially lists of sentences that I intend to do sentiment analysis on, I then use Glove vectors with 50 dim to convert the sentences into vectors with shape (sample size, 56, 50), since my maximum number of words per sentence is 56 (is this on the high side?).
My model summary:
Layer (type) Output Shape Param #
=================================================================
embedding_5 (Embedding) (None, 56, 50) 20000050
_________________________________________________________________
lstm_6 (LSTM) (None, 128) 91648
_________________________________________________________________
dense_4 (Dense) (None, 5) 645
=================================================================
Total params: 20,092,343
Trainable params: 92,293
Non-trainable params: 20,000,050
My inputs:
print(train_in.shape, train_out.shape)
>(156060, 56) (156060, 5)
emb_matrix.shape
>(400001, 50)
print(train_in.dtype, train_out.dtype, emb_matrix.dtype)
>float32 float32 float32
And finally my error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in _MaybeCompile(scope, op, func, grad_fn)
369 try:
--> 370 xla_compile = op.get_attr("_XlaCompile")
371 xla_separate_compiled_gradients = op.get_attr(
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in get_attr(self, name)
2172 raise ValueError(
-> 2173 "No attr named '" + name + "' in " + str(self._node_def))
2174 x = self._node_def.attr[name]
ValueError: No attr named '_XlaCompile' in name: "lstm_6/while/TensorArrayWrite/TensorArrayWriteV3"
op: "TensorArrayWriteV3"
input: "lstm_6/while/TensorArrayWrite/TensorArrayWriteV3/Enter"
input: "lstm_6/while/Identity_1"
input: "lstm_6/while/mul_5"
input: "lstm_6/while/Identity_2"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_class"
value {
list {
s: "loc:#lstm_6/while/mul_5"
}
}
}
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
509 as_ref=input_arg.is_ref,
--> 510 preferred_dtype=default_dtype)
511 except TypeError as err:
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1021 if ret is None:
-> 1022 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1023
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
865 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
--> 866 (dtype.name, t.dtype.name, str(t)))
867 return t
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype int64: 'Tensor("lstm_6/while/maximum_iterations:0", shape=(), dtype=int64)'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-54-936a1189c2d5> in <module>()
----> 1 model.fit(train_in, train_out, epochs = 50, batch_size = 32, shuffle=True)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1006 else:
1007 ins = x + y + sample_weights
-> 1008 self._make_train_function()
1009 f = self.train_function
1010
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\engine\training.py in _make_train_function(self)
496 training_updates = self.optimizer.get_updates(
497 params=self._collected_trainable_weights,
--> 498 loss=self.total_loss)
499 updates = (self.updates +
500 training_updates +
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\optimizers.py in get_updates(self, loss, params)
633 #interfaces.legacy_get_updates_support
634 def get_updates(self, loss, params):
--> 635 grads = self.get_gradients(loss, params)
636 self.updates = [K.update_add(self.iterations, 1)]
637
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\optimizers.py in get_gradients(self, loss, params)
87
88 def get_gradients(self, loss, params):
---> 89 grads = K.gradients(loss, params)
90 if None in grads:
91 raise ValueError('An operation has `None` for gradient. '
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\backend\tensorflow_backend.py in gradients(loss, variables)
2706 A gradients tensor.
2707 """
-> 2708 return tf.gradients(loss, variables, colocate_gradients_with_ops=True)
2709
2710
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
607 # functions.
608 in_grads = _MaybeCompile(
--> 609 grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
610 else:
611 # For function call ops, we add a 'SymbolicGradient'
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in _MaybeCompile(scope, op, func, grad_fn)
373 xla_scope = op.get_attr("_XlaScope").decode()
374 except ValueError:
--> 375 return grad_fn() # Exit early
376
377 if not xla_compile:
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in <lambda>()
607 # functions.
608 in_grads = _MaybeCompile(
--> 609 grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
610 else:
611 # For function call ops, we add a 'SymbolicGradient'
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\tensor_array_grad.py in _TensorArrayWriteGrad(op, flow)
129 colocate_with_first_write_call=False)
130 .grad(source=grad_source, flow=flow))
--> 131 grad = g.read(index)
132 return [None, None, grad, flow]
133
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\tensor_array_ops.py in read(self, index, name)
857 The tensor at index `index`.
858 """
--> 859 return self._implementation.read(index, name=name)
860
861 #tf_should_use.should_use_result
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\tensor_array_ops.py in read(self, index, name)
257 flow_in=self._flow,
258 dtype=self._dtype,
--> 259 name=name)
260 if self._element_shape:
261 value.set_shape(self._element_shape[0].dims)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gen_data_flow_ops.py in _tensor_array_read_v3(handle, index, flow_in, dtype, name)
4993 _, _, _op = _op_def_lib._apply_op_helper(
4994 "TensorArrayReadV3", handle=handle, index=index, flow_in=flow_in,
-> 4995 dtype=dtype, name=name)
4996 _result = _op.outputs[:]
4997 _inputs_flat = _op.inputs
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
785 op = g.create_op(op_type_name, inputs, output_types, name=scope,
786 input_types=input_types, attrs=attr_protos,
--> 787 op_def=op_def)
788 return output_structure, op_def.is_stateful, op
789
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
3158 input_types=input_types,
3159 original_op=self._default_original_op,
-> 3160 op_def=op_def)
3161 self._create_op_helper(ret, compute_shapes=compute_shapes,
3162 compute_device=compute_device)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1672 control_flow_util.CheckInputFromValidContext(self, input_tensor.op)
1673 if self._control_flow_context is not None:
-> 1674 self._control_flow_context.AddOp(self)
1675 self._recompute_node_def()
1676
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in AddOp(self, op)
2249 op_input_ctxt._AddOpInternal(op)
2250 return
-> 2251 self._AddOpInternal(op)
2252
2253 def _AddOpInternal(self, op):
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in _AddOpInternal(self, op)
2272 for index in range(len(op.inputs)):
2273 x = op.inputs[index]
-> 2274 real_x = self.AddValue(x)
2275 if real_x != x:
2276 op._update_input(index, real_x)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in AddValue(self, val)
2205 forward_ctxt = forward_ctxt.GetWhileContext()
2206 if forward_ctxt == grad_ctxt.grad_state.forward_context:
-> 2207 real_val = grad_ctxt.grad_state.GetRealValue(val)
2208 self._external_values[val.name] = real_val
2209 return real_val
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in GetRealValue(self, value)
1048 # Record the history of this value in forward_ctxt.
1049 self._grad_context.Exit()
-> 1050 history_value = cur_grad_state.AddForwardAccumulator(cur_value)
1051 self._grad_context.Enter()
1052 break
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in AddForwardAccumulator(self, value, dead_branch)
906 max_size=maximum_iterations,
907 elem_type=value.dtype.base_dtype,
--> 908 name="f_acc")
909 # pylint: enable=protected-access
910 if curr_ctxt: curr_ctxt.Exit()
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gen_data_flow_ops.py in _stack_v2(max_size, elem_type, stack_name, name)
4014 _, _, _op = _op_def_lib._apply_op_helper(
4015 "StackV2", max_size=max_size, elem_type=elem_type,
-> 4016 stack_name=stack_name, name=name)
4017 _result = _op.outputs[:]
4018 _inputs_flat = _op.inputs
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
531 if input_arg.type != types_pb2.DT_INVALID:
532 raise TypeError("%s expected type of %s." %
--> 533 (prefix, dtypes.as_dtype(input_arg.type).name))
534 else:
535 # Update the maps with the default, if needed.
TypeError: Input 'max_size' of 'StackV2' Op has type int64 that does not match expected type of int32.

I was using version 1.5.0 of TF initially, upgraded to v1.8.0 and all is working. Issue resolved.

Related

TF 2.6: Input 'resource' of 'AssignVariableOp' Op has type float32 that does not match expected type of resource

I am applying an autoencoder to categorical data. My code used to work just fine, but after updating to TF 2.6, it throws this error while attempting to save my model: Input 'resource' of 'AssignVariableOp' Op has type float32 that does not match expected type of resource.
The data file can be found here: https://drive.google.com/file/d/1Jnblr3Ik88V_qd3N5EDkTkBtFu_E7ku1/view?usp=sharing
It is in tsv format.
Here is the code and full error traceback:
import csv
import sys
import numpy as np
import math
import os
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input, Embedding, Flatten, Reshape, Dropout
from tensorflow.keras.regularizers import l1
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
import tensorflow.keras.callbacks as kcb
def adjust_count(x) :
if x<2 :
return x
else :
return x-1
mutations = {}
mut_info = {}
sampleIDs = []
mapping = {
"0|0":0,
"0|1":1,
"1|0":2,
"1|1":3
}
SNP_vcf = "/my/dir/in/chr1_SNPs_50785418_51253612_fh.vcf"
outdir = "/my/dir/out/"
threshold = 1
chromosome, foo, start, end, _ = SNP_vcf.split('/').pop().split('.').pop(0).split('_') # foo will be "SNPs"
start=int(start)
end=int(end)
with open(SNP_vcf, newline='') as csvfile:
csvread = csv.reader(csvfile, delimiter='\t', quotechar='"')
for myline in csvread: #reads csvfile line by line, storing array of cells into myline
if(myline[0][0] == '#'):
del(myline[0:9])
for mycell in myline :
sampleIDs.append(mycell)
else :
chrom, pos, notID, ref, alt = myline[0:5] #get info of the SNP
ID = chrom + "_" + pos + "." + ref + "_" + alt
chrom = int(chrom)
pos = int(pos) #these will be stored as str otherwise
info = myline[7].split(';') # get info stored as string
dinfo = {} # we need 'AF', not always at same position in string
for v in info : # so convert the list into a dict to get it after
t = v.split('=')
if(len(t)>1) :
dinfo[t[0]] = t[1]
mut_info[ID] = [[chrom, pos], #position info
[ref, alt], #nucletodie info
min(float(dinfo['AF']), 1-float(dinfo['AF'])), #global allele frequency (min(x, 1-x) because some have freq>50%)
-1]
mutations[ID] = [] #will be used to store the mutation in pop data
del(myline[0:9])
for mycell in myline :
mutations[ID].append(mapping[mycell]) # ONE HOT ENCODING
mut_info[ID][3] = sum(map(adjust_count, mutations[ID]))
print("Read "+str(len(mutations))+" mutations from "+str(start)+" to "+str(end)+" on "+ chromosome)
tokeep = []
if threshold < 1 :
for k,v in mut_info.items() :
if v[2] >= threshold :
tokeep.append(k)
else:
for k,v in mut_info.items() :
if v[3] >= threshold :
tokeep.append(k)
print(str(len(tokeep))+" mutations passed the frequency threshold of "+str(threshold))
x = []
for k in tokeep :
x.append(mutations[k])
x_arr = np.array(x)
x_tr = np.transpose(x_arr)
test_index = np.random.choice(range(len(x_tr)), math.floor(len(x_tr)/5), replace=False)
x_test = to_categorical(x_tr[test_index])
x_train = to_categorical(np.delete(x_tr, test_index, 0))
print("Total data is of shape "+str(x_tr.shape))
print("Training data is of shape "+str(x_train.shape))
print("Testing data is of shape " + str(x_test.shape))
input_size = len(tokeep)
hidden_1_size = math.ceil(0.5*input_size)
hidden_2_size = math.ceil(0.2*input_size)
code_size = math.ceil(0.05*input_size)
print("Input size: "+str(input_size))
print("First hidden layer size: "+str(hidden_1_size))
print("Second hidden layer size: "+str(hidden_2_size))
print("Code size: "+str(code_size))
input_data = Input(shape=x_train[1].shape)
f = Flatten()(input_data)
hidden_1 = Dense(hidden_1_size, activation='relu')(f)
e_drop = Dropout(0.4)(hidden_1)
hidden_2 = Dense(hidden_2_size, activation='relu')(e_drop)
code = Dense(code_size, activation='relu')(hidden_2)
hidden_2_rev = Dense(hidden_2_size, activation='relu')(code)
d_drop = Dropout(0.4)(hidden_2_rev)
hidden_1_rev = Dense(hidden_1_size, activation='relu')(d_drop)
output_data = Dense(input_size*4, activation='softmax')(hidden_1_rev)
rshp_output_data = Reshape((-1,4))(output_data)
autoencoder = Model(input_data, rshp_output_data)
encoder = Model(input_data, code)
encoded_input = Input(shape=(code_size,))
decoder = Model(encoded_input, autoencoder.layers[-1](autoencoder.layers[-2](autoencoder.layers[-3](autoencoder.layers[-4](autoencoder.layers[-5](encoded_input))))) )
autoencoder.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[tf.keras.metrics.CategoricalAccuracy()]) # when data is OHE
history = autoencoder.fit( x_train, x_train, epochs=1000, shuffle=True, validation_data=(x_test, x_test),
callbacks=kcb.EarlyStopping(monitor="val_loss", patience=3, restore_best_weights=True) )
encoder.save(outdir + "encoder_fh_"+chromosome+"_"+str(start)+"_"+str(end))
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
516 else:
--> 517 values = ops.convert_to_tensor(
518 values,
~/.local/lib/python3.8/site-packages/tensorflow/python/profiler/trace.py in wrapped(*args, **kwargs)
162 return func(*args, **kwargs)
--> 163 return func(*args, **kwargs)
164
~/.local/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1532 if dtype is not None and not dtype.is_compatible_with(value.dtype):
-> 1533 raise ValueError(
1534 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
ValueError: Tensor conversion requested dtype resource for Tensor with dtype float32: <tf.Tensor 'dense_6/kernel/Read/ReadVariableOp:0' shape=(10000, 1250) dtype=float32>
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-16-e76ca5687b87> in <module>
1 ## SAVE OUPUT
2 # autoencoder.save(outdir + "autoencoder_"+chromosome+"_"+str(start)+"_"+str(end))
----> 3 encoder.save(outdir + "encoder_fh_"+chromosome+"_"+str(start)+"_"+str(end))
~/.local/lib/python3.8/site-packages/keras/engine/training.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures, options, save_traces)
2143 """
2144 # pylint: enable=line-too-long
-> 2145 save.save_model(self, filepath, overwrite, include_optimizer, save_format,
2146 signatures, options, save_traces)
2147
~/.local/lib/python3.8/site-packages/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options, save_traces)
147 else:
148 with generic_utils.SharedObjectSavingScope():
--> 149 saved_model_save.save(model, filepath, overwrite, include_optimizer,
150 signatures, options, save_traces)
151
~/.local/lib/python3.8/site-packages/keras/saving/saved_model/save.py in save(model, filepath, overwrite, include_optimizer, signatures, options, save_traces)
88 with K.deprecated_internal_learning_phase_scope(0):
89 with utils.keras_option_scope(save_traces):
---> 90 saved_nodes, node_paths = save_lib.save_and_return_nodes(
91 model, filepath, signatures, options)
92
~/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in save_and_return_nodes(obj, export_dir, signatures, options, experimental_skip_checkpoint)
1226
1227 _, exported_graph, object_saver, asset_info, saved_nodes, node_paths = (
-> 1228 _build_meta_graph(obj, signatures, options, meta_graph_def))
1229 saved_model.saved_model_schema_version = (
1230 pywrap_libexport.SAVED_MODEL_SCHEMA_VERSION)
~/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in _build_meta_graph(obj, signatures, options, meta_graph_def)
1397
1398 with save_context.save_context(options):
-> 1399 return _build_meta_graph_impl(obj, signatures, options, meta_graph_def)
~/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in _build_meta_graph_impl(obj, signatures, options, meta_graph_def)
1349 wrapped_functions)
1350 object_saver = util.TrackableSaver(checkpoint_graph_view)
-> 1351 asset_info, exported_graph = _fill_meta_graph_def(
1352 meta_graph_def, saveable_view, signatures,
1353 options.namespace_whitelist, options.experimental_custom_gradients)
~/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in _fill_meta_graph_def(meta_graph_def, saveable_view, signature_functions, namespace_whitelist, save_custom_gradients)
872 "to get the full error message.")
873
--> 874 saver_def = saver.to_proto()
875 meta_graph_def.saver_def.CopyFrom(saver_def)
876 graph_def = exported_graph.as_graph_def(add_shapes=True)
~/.local/lib/python3.8/site-packages/tensorflow/python/training/saving/functional_saver.py in to_proto(self)
180 shape=[], dtype=dtypes.string, name="saver_filename")
181 save_tensor = self._traced_save(filename_tensor)
--> 182 restore_op = self._traced_restore(filename_tensor).op
183 return saver_pb2.SaverDef(
184 filename_tensor_name=filename_tensor.name,
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
883
884 with OptionalXlaContext(self._jit_compile):
--> 885 result = self._call(*args, **kwds)
886
887 new_tracing_count = self.experimental_get_tracing_count()
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
931 # This is the first call of __call__, so we have to initialize.
932 initializers = []
--> 933 self._initialize(args, kwds, add_initializers_to=initializers)
934 finally:
935 # At this point we know that the initialization is complete (or less
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
757 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
758 self._concrete_stateful_fn = (
--> 759 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
760 *args, **kwds))
761
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
3064 args, kwargs = None, None
3065 with self._lock:
-> 3066 graph_function, _ = self._maybe_define_function(args, kwargs)
3067 return graph_function
3068
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
3461
3462 self._function_cache.missed.add(call_context_key)
-> 3463 graph_function = self._create_graph_function(args, kwargs)
3464 self._function_cache.primary[cache_key] = graph_function
3465
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3296 arg_names = base_arg_names + missing_arg_names
3297 graph_function = ConcreteFunction(
-> 3298 func_graph_module.func_graph_from_py_func(
3299 self._name,
3300 self._python_function,
~/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes, acd_record_initial_resource_uses)
1005 _, original_func = tf_decorator.unwrap(python_func)
1006
-> 1007 func_outputs = python_func(*func_args, **func_kwargs)
1008
1009 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
666 # the function a weak reference to itself to avoid a reference cycle.
667 with OptionalXlaContext(compile_with_xla):
--> 668 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
669 return out
670
~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in bound_method_wrapper(*args, **kwargs)
3983 if tf_inspect.ismethod(wrapped_fn):
3984 wrapped_fn = six.get_unbound_function(wrapped_fn)
-> 3985 return wrapped_fn(weak_instance(), *args, **kwargs)
3986
3987 # If __wrapped__ was replaced, then it is always an unbound function.
~/.local/lib/python3.8/site-packages/tensorflow/python/training/saving/functional_saver.py in _traced_restore(self, file_prefix)
200 autograph=False)
201 def _traced_restore(self, file_prefix):
--> 202 restore_ops = self.restore(file_prefix)
203 with ops.device("cpu:0"):
204 with ops.control_dependencies(restore_ops.values()):
~/.local/lib/python3.8/site-packages/tensorflow/python/training/saving/functional_saver.py in restore(self, file_prefix, options)
337 restore_ops = tf_function_restore()
338 else:
--> 339 restore_ops = restore_fn()
340
341 for callback in self._after_restore_callbacks:
~/.local/lib/python3.8/site-packages/tensorflow/python/training/saving/functional_saver.py in restore_fn()
321 for device, saver in sorted(self._single_device_savers.items()):
322 with ops.device(device):
--> 323 restore_ops.update(saver.restore(file_prefix, options))
324
325 return restore_ops
~/.local/lib/python3.8/site-packages/tensorflow/python/training/saving/functional_saver.py in restore(self, file_prefix, options)
113 for saveable, restored_tensors in zip(self._saveable_objects,
114 structured_restored_tensors):
--> 115 restore_ops[saveable.name] = saveable.restore(
116 restored_tensors, restored_shapes=None)
117 return restore_ops
~/.local/lib/python3.8/site-packages/tensorflow/python/training/saving/saveable_object_util.py in restore(self, restored_tensors, restored_shapes)
129 with ops.device(self._var_device):
130 restored_tensor = array_ops.identity(restored_tensor)
--> 131 return resource_variable_ops.shape_safe_assign_variable_handle(
132 self.handle_op, self._var_shape, restored_tensor)
133
~/.local/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py in shape_safe_assign_variable_handle(handle, shape, value, name)
308 value_tensor = ops.convert_to_tensor(value)
309 shape.assert_is_compatible_with(value_tensor.shape)
--> 310 return gen_resource_variable_ops.assign_variable_op(
311 handle, value_tensor, name=name)
312
~/.local/lib/python3.8/site-packages/tensorflow/python/ops/gen_resource_variable_ops.py in assign_variable_op(resource, value, name)
152 pass # Add nodes to the TensorFlow graph.
153 # Add nodes to the TensorFlow graph.
--> 154 _, _, _op, _outputs = _op_def_library._apply_op_helper(
155 "AssignVariableOp", resource=resource, value=value, name=name)
156 return _op
~/.local/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
542 (input_name, op_type_name, observed))
543 if input_arg.type != types_pb2.DT_INVALID:
--> 544 raise TypeError("%s expected type of %s." %
545 (prefix, dtypes.as_dtype(input_arg.type).name))
546 else:
TypeError: Input 'resource' of 'AssignVariableOp' Op has type float32 that does not match expected type of resource.
I tried specifying the dtype when creating x_train and x_test, but it does not affect this type mismatch error. What can I do to fix it?

Keras Concatenate layer dimensions acting up

So I'm creating a model using the functional API in tf.keras in which I'm doing a multi-input model.
The input for training is of shape (n_examples = 58667, n_dim = 2748). Each example is a concatenate of a 2048 and a 700 dimensions vector.
But I'm getting an error message that I don't understand:
InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 1 and 0. Shapes are [1] and [0]. for 'model_27/concatenate_28/concat' (op: 'ConcatV2') with input shapes: [1,100], [0,100], [] and with computed input tensors: input[2] = <1>.
Here are dummy inputs and imports to make it runnable:
from tensorflow.keras import models, layers, losses, metrics, optimizers
from tensorflow.keras.layers import Dense, Concatenate, Input, Lambda
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
from sklearn.model_selection import train_test_split
import numpy as np
fake_train = np.random.rand(10000,2748)
fake_test = np.random.randint(0,1,(10000,1))
x_train, x_dev, y_train, y_dev = train_test_split(fake_train, fake_test, test_size = 0.2)
My model is created with this function:
def build_model():
input0 = Input(shape=(2748,))
branch1 = Lambda(lambda x:x[:2048])(input0)
branch1 = Dense(1000, activation='relu')(branch1)
branch1 = Dense(100, activation='relu')(branch1)
branch1 = Dense(100, activation='relu')(branch1)
branch2 = Lambda(lambda x:x[2048:])(input0)
branch2 = Dense(1000, activation='relu')(branch2)
branch2 = Dense(100, activation='relu')(branch2)
branch2 = Dense(100, activation='relu')(branch2)
out = layers.concatenate([branch1, branch2],axis=-1)
out = Dense(10, activation = 'relu')(out)
out = Dense(1, activation='sigmoid')(out)
model = Model(inputs=input0, outputs=out)
model.compile(optimizer=optimizers.Adam(lr=0.001),
loss='binary_crossentropy',
metrics=['accuracy', recall_m, precision_m])
return model
Here are parameters for cross validation for the dummy data:
k = 3 #Number of folds for CV
num_epochs = 4 #for test only
batch_size = 1
And this is my cross-validation for the model, which started the error:
all_loss_histories = []
all_recall_histories = []
all_precision_histories = []
for i in range(k):
val_data = x_train[i * num_val_samples:(i+1) * num_val_samples]
val_targets = y_train[i * num_val_samples:(i+1) * num_val_samples]
partial_train_data = np.concatenate(
[x_train[:i*num_val_samples],
x_train[(i+1)*num_val_samples:]],
axis = 0)
partial_train_targets = np.concatenate(
[y_train[:i*num_val_samples],
y_train[(i+1)*num_val_samples:]],
axis = 0)
model = build_model()
history = model.fit(partial_train_data,
partial_train_targets,
epochs = num_epochs,
batch_size = batch_size,
verbose = 1,
validation_data = (val_data, val_targets),
use_multiprocessing=False)
print('Finished training fold '+str(i+1))
loss_history = history.history['val_loss']
recall_history = history.history['val_recall_m']
precision_history = history.history['val_precision_m']
all_loss_histories.append(loss_history)
all_recall_histories.append(recall_history)
all_precision_histories.append(precision_history)
Any idea why there is an error?
Using python3.7 and tf 2.0 on a MacBook Pro 2018 (on OSX, not on a linux VM)
Thanks!
The complete error:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1609 try:
-> 1610 c_op = c_api.TF_FinishOperation(op_desc)
1611 except errors.InvalidArgumentError as e:
InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 1 and 0. Shapes are [1] and [0]. for 'model_33/concatenate_34/concat' (op: 'ConcatV2') with input shapes: [1,100], [0,100], [] and with computed input tensors: input[2] = <1>.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<timed exec> in <module>
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
726 max_queue_size=max_queue_size,
727 workers=workers,
--> 728 use_multiprocessing=use_multiprocessing)
729
730 def evaluate(self,
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
322 mode=ModeKeys.TRAIN,
323 training_context=training_context,
--> 324 total_epochs=epochs)
325 cbks.make_logs(model, epoch_logs, training_result, ModeKeys.TRAIN)
326
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in run_one_epoch(model, iterator, execution_function, dataset_size, batch_size, strategy, steps_per_epoch, num_samples, mode, training_context, total_epochs)
121 step=step, mode=mode, size=current_batch_size) as batch_logs:
122 try:
--> 123 batch_outs = execution_function(iterator)
124 except (StopIteration, errors.OutOfRangeError):
125 # TODO(kaftan): File bug about tf function and errors.OutOfRangeError?
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in execution_function(input_fn)
84 # `numpy` translates Tensors to values in Eager mode.
85 return nest.map_structure(_non_none_constant_value,
---> 86 distributed_function(input_fn))
87
88 return execution_function
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in __call__(self, *args, **kwds)
455
456 tracing_count = self._get_tracing_count()
--> 457 result = self._call(*args, **kwds)
458 if tracing_count == self._get_tracing_count():
459 self._call_counter.called_without_tracing()
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _call(self, *args, **kwds)
501 # This is the first call of __call__, so we have to initialize.
502 initializer_map = object_identity.ObjectIdentityDictionary()
--> 503 self._initialize(args, kwds, add_initializers_to=initializer_map)
504 finally:
505 # At this point we know that the initialization is complete (or less
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
406 self._concrete_stateful_fn = (
407 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 408 *args, **kwds))
409
410 def invalid_creator_scope(*unused_args, **unused_kwds):
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
1846 if self.input_signature:
1847 args, kwargs = None, None
-> 1848 graph_function, _, _ = self._maybe_define_function(args, kwargs)
1849 return graph_function
1850
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _maybe_define_function(self, args, kwargs)
2148 graph_function = self._function_cache.primary.get(cache_key, None)
2149 if graph_function is None:
-> 2150 graph_function = self._create_graph_function(args, kwargs)
2151 self._function_cache.primary[cache_key] = graph_function
2152 return graph_function, args, kwargs
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
2039 arg_names=arg_names,
2040 override_flat_arg_shapes=override_flat_arg_shapes,
-> 2041 capture_by_value=self._capture_by_value),
2042 self._function_attributes,
2043 # Tell the ConcreteFunction to clean up its graph once it goes out of
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
913 converted_func)
914
--> 915 func_outputs = python_func(*func_args, **func_kwargs)
916
917 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py in wrapped_fn(*args, **kwds)
356 # __wrapped__ allows AutoGraph to swap in a converted function. We give
357 # the function a weak reference to itself to avoid a reference cycle.
--> 358 return weak_wrapped_fn().__wrapped__(*args, **kwds)
359 weak_wrapped_fn = weakref.ref(wrapped_fn)
360
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in distributed_function(input_iterator)
71 strategy = distribution_strategy_context.get_strategy()
72 outputs = strategy.experimental_run_v2(
---> 73 per_replica_function, args=(model, x, y, sample_weights))
74 # Out of PerReplica outputs reduce or pick values to return.
75 all_outputs = dist_utils.unwrap_output_dict(
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/distribute/distribute_lib.py in experimental_run_v2(self, fn, args, kwargs)
758 fn = autograph.tf_convert(fn, ag_ctx.control_status_ctx(),
759 convert_by_default=False)
--> 760 return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
761
762 def reduce(self, reduce_op, value, axis):
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/distribute/distribute_lib.py in call_for_each_replica(self, fn, args, kwargs)
1785 kwargs = {}
1786 with self._container_strategy().scope():
-> 1787 return self._call_for_each_replica(fn, args, kwargs)
1788
1789 def _call_for_each_replica(self, fn, args, kwargs):
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/distribute/distribute_lib.py in _call_for_each_replica(self, fn, args, kwargs)
2130 self._container_strategy(),
2131 replica_id_in_sync_group=constant_op.constant(0, dtypes.int32)):
-> 2132 return fn(*args, **kwargs)
2133
2134 def _reduce_to(self, reduce_op, value, destinations):
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py in wrapper(*args, **kwargs)
290 def wrapper(*args, **kwargs):
291 with ag_ctx.ControlStatusCtx(status=ag_ctx.Status.DISABLED):
--> 292 return func(*args, **kwargs)
293
294 if inspect.isfunction(func) or inspect.ismethod(func):
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2_utils.py in train_on_batch(model, x, y, sample_weight, class_weight, reset_metrics)
262 y,
263 sample_weights=sample_weights,
--> 264 output_loss_metrics=model._output_loss_metrics)
265
266 if reset_metrics:
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_eager.py in train_on_batch(model, inputs, targets, sample_weights, output_loss_metrics)
309 sample_weights=sample_weights,
310 training=True,
--> 311 output_loss_metrics=output_loss_metrics))
312 if not isinstance(outs, list):
313 outs = [outs]
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_eager.py in _process_single_batch(model, inputs, targets, output_loss_metrics, sample_weights, training)
250 output_loss_metrics=output_loss_metrics,
251 sample_weights=sample_weights,
--> 252 training=training))
253 if total_loss is None:
254 raise ValueError('The model cannot be run '
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_eager.py in _model_loss(model, inputs, targets, output_loss_metrics, sample_weights, training)
125 inputs = nest.map_structure(ops.convert_to_tensor, inputs)
126
--> 127 outs = model(inputs, **kwargs)
128 outs = nest.flatten(outs)
129
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
845 outputs = base_layer_utils.mark_as_return(outputs, acd)
846 else:
--> 847 outputs = call_fn(cast_inputs, *args, **kwargs)
848
849 except errors.OperatorNotAllowedInGraphError as e:
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in call(self, inputs, training, mask)
706 return self._run_internal_graph(
707 inputs, training=training, mask=mask,
--> 708 convert_kwargs_to_constants=base_layer_utils.call_context().saving)
709
710 def compute_output_shape(self, input_shape):
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in _run_internal_graph(self, inputs, training, mask, convert_kwargs_to_constants)
858
859 # Compute outputs.
--> 860 output_tensors = layer(computed_tensors, **kwargs)
861
862 # Update tensor_dict.
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
845 outputs = base_layer_utils.mark_as_return(outputs, acd)
846 else:
--> 847 outputs = call_fn(cast_inputs, *args, **kwargs)
848
849 except errors.OperatorNotAllowedInGraphError as e:
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/merge.py in call(self, inputs)
180 return y
181 else:
--> 182 return self._merge_function(inputs)
183
184 #tf_utils.shape_type_conversion
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/layers/merge.py in _merge_function(self, inputs)
392
393 def _merge_function(self, inputs):
--> 394 return K.concatenate(inputs, axis=self.axis)
395
396 #tf_utils.shape_type_conversion
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py in concatenate(tensors, axis)
2706 return sparse_ops.sparse_concat(axis, tensors)
2707 else:
-> 2708 return array_ops.concat([to_dense(x) for x in tensors], axis)
2709
2710
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/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
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py in concat(values, axis, name)
1429 dtype=dtypes.int32).get_shape().assert_has_rank(0)
1430 return identity(values[0], name=name)
-> 1431 return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
1432
1433
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_array_ops.py in concat_v2(values, axis, name)
1255 _attr_N = len(values)
1256 _, _, _op = _op_def_lib._apply_op_helper(
-> 1257 "ConcatV2", values=values, axis=axis, name=name)
1258 _result = _op.outputs[:]
1259 _inputs_flat = _op.inputs
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
791 op = g.create_op(op_type_name, inputs, dtypes=None, name=scope,
792 input_types=input_types, attrs=attr_protos,
--> 793 op_def=op_def)
794 return output_structure, op_def.is_stateful, op
795
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py in create_op(***failed resolving arguments***)
546 return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
547 op_type, inputs, dtypes, input_types, name, attrs, op_def,
--> 548 compute_device)
549
550 def capture(self, tensor, name=None):
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
3427 input_types=input_types,
3428 original_op=self._default_original_op,
-> 3429 op_def=op_def)
3430 self._create_op_helper(ret, compute_device=compute_device)
3431 return ret
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1771 op_def, inputs, node_def.attr)
1772 self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1773 control_input_ops)
1774 # pylint: enable=protected-access
1775
~/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1611 except errors.InvalidArgumentError as e:
1612 # Convert to ValueError for backwards compatibility.
-> 1613 raise ValueError(str(e))
1614
1615 return c_op
ValueError: Dimension 0 in both shapes must be equal, but are 1 and 0. Shapes are [1] and [0]. for 'model_33/concatenate_34/concat' (op: 'ConcatV2') with input shapes: [1,100], [0,100], [] and with computed input tensors: input[2] = <1>.```
I believe you want
Lambda(lambda x:x[:, :2048])(input0)
and
Lambda(lambda x:x[:,2048:])(input0)
since there is a batch axis which should be preserved. Your current code is splitting around 2048 along the batch axis, which results in one input to concatenate that has batch size 0, and one input that has batch size 1. Since the batch sizes don't match, they can't be concatenated.
Making this correction allows me to run your code without the error.

Tensor conversion requested dtype int32 for Tensor with dtype int64 - while estimator.export_savedmodel

Trying to export a model built using https://colab.research.google.com/github/google-research/bert/blob/master/predicting_movie_reviews_with_bert_on_tf_hub.ipynb with this:
def serving_input_fn():
with tf.variable_scope("bert_model"):
feature_spec = {
"input_ids": tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
"input_mask": tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
"segment_ids": tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
"label_ids": tf.FixedLenFeature([], tf.int64),
}
serialized_tf_example = tf.placeholder(dtype=tf.string,
shape=[None],
name='input_example_tensor')
receiver_tensors = {'examples': serialized_tf_example}
features = tf.parse_example(serialized_tf_example, feature_spec)
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
MODEL_DIR = 'gs://{}/bert/models_servable/{}'.format(BUCKET,'bert')
tf.gfile.MakeDirs(MODEL_DIR)
estimator._export_to_tpu = False
model_file = os.path.join(MODEL_DIR, "bert_model")
path = estimator.export_savedmodel(model_file, serving_input_fn)
print(path)
and it gives following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-106-aaf5ee490ed7> in <module>()
21 model_file = os.path.join(MODEL_DIR, "bert_model")
22 print(model_file)
---> 23 path = estimator.export_savedmodel(model_file, serving_input_fn)
24 print(path)
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in export_savedmodel(self, export_dir_base, serving_input_receiver_fn, assets_extra, as_text, checkpoint_path, strip_default_attrs)
1643 as_text=as_text,
1644 checkpoint_path=checkpoint_path,
-> 1645 experimental_mode=model_fn_lib.ModeKeys.PREDICT)
1646
1647 def export_saved_model(
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in export_saved_model(self, export_dir_base, serving_input_receiver_fn, assets_extra, as_text, checkpoint_path, experimental_mode)
721 assets_extra=assets_extra,
722 as_text=as_text,
--> 723 checkpoint_path=checkpoint_path)
724
725 def experimental_export_all_saved_models(
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in experimental_export_all_saved_models(self, export_dir_base, input_receiver_fn_map, assets_extra, as_text, checkpoint_path)
825 self._add_meta_graph_for_mode(
826 builder, input_receiver_fn_map, checkpoint_path,
--> 827 save_variables, mode=model_fn_lib.ModeKeys.PREDICT)
828 save_variables = False
829
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in _add_meta_graph_for_mode(self, builder, input_receiver_fn_map, checkpoint_path, save_variables, mode, export_tags, check_variables)
895 labels=getattr(input_receiver, 'labels', None),
896 mode=mode,
--> 897 config=self.config)
898
899 export_outputs = model_fn_lib.export_outputs_for_mode(
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
1110
1111 logging.info('Calling model_fn.')
-> 1112 model_fn_results = self._model_fn(features=features, **kwargs)
1113 logging.info('Done calling model_fn.')
1114
<ipython-input-90-119a3167bf33> in model_fn(features, labels, mode, params)
72 else:
73 (predicted_labels, log_probs) = create_model(
---> 74 is_predicting, input_ids, input_mask, segment_ids, label_ids, num_labels)
75
76 predictions = {
<ipython-input-89-0f7bd7d1be35> in create_model(is_predicting, input_ids, input_mask, segment_ids, labels, num_labels)
13 inputs=bert_inputs,
14 signature="tokens",
---> 15 as_dict=True)
16
17 # Use "pooled_output" for classification tasks on an entire sentence.
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/module.py in __call__(self, inputs, _sentinel, signature, as_dict)
240 dict_inputs = _convert_dict_inputs(
241 inputs, self._spec.get_input_info_dict(signature=signature,
--> 242 tags=self._tags))
243
244 dict_outputs = self._impl.create_apply_graph(
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/module.py in _convert_dict_inputs(inputs, tensor_info_map)
442 dict_inputs = _prepare_dict_inputs(inputs, tensor_info_map)
443 return tensor_info.convert_dict_to_compatible_tensor(dict_inputs,
--> 444 tensor_info_map)
445
446
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/tensor_info.py in convert_dict_to_compatible_tensor(values, targets)
146 for key, value in sorted(values.items()):
147 result[key] = _convert_to_compatible_tensor(
--> 148 value, targets[key], error_prefix="Can't convert %r" % key)
149 return result
150
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/tensor_info.py in _convert_to_compatible_tensor(value, target, error_prefix)
115 """
116 try:
--> 117 tensor = tf.convert_to_tensor_or_indexed_slices(value, target.dtype)
118 except TypeError as e:
119 raise TypeError("%s: %s" % (error_prefix, e))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor_or_indexed_slices(value, dtype, name)
1296 """
1297 return internal_convert_to_tensor_or_indexed_slices(
-> 1298 value=value, dtype=dtype, name=name, as_ref=False)
1299
1300
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor_or_indexed_slices(value, dtype, name, as_ref)
1330 raise ValueError(
1331 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1332 (dtypes.as_dtype(dtype).name, value.dtype.name, str(value)))
1333 return value
1334 else:
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype string: 'Tensor("bert_model/ParseExample/ParseExample:0", shape=(?, 128), dtype=string)'
The int32 coercing is for one or all of the features: input_ids, input_mask, segment_ids and label_ids.
The model code only has an int32 conversion, which doesn't seem to be causing this
one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)
predicted_labels = tf.squeeze(tf.argmax(log_probs, axis=-1, output_type=tf.int32))
Where is the int32 coercing happening and how to fix? Thanks in advance!
the bert model require input_ids,input_mask and segment_ids as type of tf.int32. In order to fix the bug, you have to convert them from tf.int64 to tf.int32 as below
def create_model(is_predicting, input_ids, input_mask, segment_ids, labels, num_labels):
"""Creates a classification model."""
input_ids = tf.cast(input_ids, tf.int32)
input_mask = tf.cast(input_mask, tf.int32)
segment_ids = tf.cast(segment_ids, tf.int32)
bert_module = hub.Module(
BERT_MODEL_HUB,
trainable=True)

ValueError: Dimensions must be equal, but are 512 and 256

I am trying to implement seq2seq model for text summarization using Tensorflow 1.3.0.
I am trying to use MultiRNNCell and bidirectional_dynamic_rnn in encoding layer. I am missing something, but unable to find it. The error stack trace is not straight forward which makes it more difficult to understand.
I am getting below error while building the Graph.
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
653 graph_def_version, node_def_str, input_shapes, input_tensors,
--> 654 input_tensors_as_shapes, status)
655 except errors.InvalidArgumentError as err:
~/anaconda2/envs/tensorflow/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback)
65 try:
---> 66 next(self.gen)
67 except StopIteration:
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
465 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466 pywrap_tensorflow.TF_GetCode(status))
467 finally:
InvalidArgumentError: Dimensions must be equal, but are 512 and 256 for 'decoding/decoder/while/BasicDecoderStep/decoder/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/mul' (op: 'Mul') with input shapes: [?,512], [?,256].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-119-85ee67bc88e5> in <module>()
9 # Create the training and inference logits
10 training_logits, inference_logits = seq2seq_model(input_,target,embeding_matrix,vocab_to_int,source_seq_length,target_seq_length,
---> 11 max_target_seq_length,rnn_size,keep_probability,num_layers,batch_size)
12
13 # Create tensors for the training logits and inference logits
<ipython-input-114-5ad1bf459bd7> in seq2seq_model(source_input, target_input, embeding_matrix, vocab_to_int, source_sequence_length, target_sequence_length, max_target_length, rnn_size, keep_prob, num_layers, batch_size)
15 training_logits, inference_logits = decoding_layer(target_input,encoder_states,embedings,
16 vocab_to_int,rnn_size,target_sequence_length,
---> 17 max_target_length,batch_size,num_layers)
18
19 return training_logits, inference_logits
<ipython-input-113-c2b4542605d2> in decoding_layer(target_inputs, encoder_state, embedding, vocab_to_int, rnn_size, target_sequence_length, max_target_length, batch_size, num_layers)
12
13 training_logits = training_decoder(embed,decoder_cell,encoder_state,output_layer,
---> 14 target_sequence_length,max_target_length)
15
16
<ipython-input-117-012bbcdcf997> in training_decoder(dec_embed_input, decoder_cell, encoder_state, output_layer, target_sequence_length, max_target_length)
17
18 final_outputs, final_state = tf.contrib.seq2seq.dynamic_decode(decoder=decoder,impute_finished=True,
---> 19 maximum_iterations=max_target_length)
20
21 return final_outputs
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/seq2seq/python/ops/decoder.py in dynamic_decode(decoder, output_time_major, impute_finished, maximum_iterations, parallel_iterations, swap_memory, scope)
284 ],
285 parallel_iterations=parallel_iterations,
--> 286 swap_memory=swap_memory)
287
288 final_outputs_ta = res[1]
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name)
2773 context = WhileContext(parallel_iterations, back_prop, swap_memory, name)
2774 ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, context)
-> 2775 result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
2776 return result
2777
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)
2602 self.Enter()
2603 original_body_result, exit_vars = self._BuildLoop(
-> 2604 pred, body, original_loop_vars, loop_vars, shape_invariants)
2605 finally:
2606 self.Exit()
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)
2552 structure=original_loop_vars,
2553 flat_sequence=vars_for_body_with_tensor_arrays)
-> 2554 body_result = body(*packed_vars_for_body)
2555 if not nest.is_sequence(body_result):
2556 body_result = [body_result]
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/seq2seq/python/ops/decoder.py in body(time, outputs_ta, state, inputs, finished, sequence_lengths)
232 """
233 (next_outputs, decoder_state, next_inputs,
--> 234 decoder_finished) = decoder.step(time, inputs, state)
235 next_finished = math_ops.logical_or(decoder_finished, finished)
236 if maximum_iterations is not None:
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/contrib/seq2seq/python/ops/basic_decoder.py in step(self, time, inputs, state, name)
137 """
138 with ops.name_scope(name, "BasicDecoderStep", (time, inputs, state)):
--> 139 cell_outputs, cell_state = self._cell(inputs, state)
140 if self._output_layer is not None:
141 cell_outputs = self._output_layer(cell_outputs)
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/rnn_cell_impl.py in __call__(self, inputs, state, scope)
178 with vs.variable_scope(vs.get_variable_scope(),
179 custom_getter=self._rnn_get_variable):
--> 180 return super(RNNCell, self).__call__(inputs, state)
181
182 def _rnn_get_variable(self, getter, *args, **kwargs):
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/layers/base.py in __call__(self, inputs, *args, **kwargs)
448 # Check input assumptions set after layer building, e.g. input shape.
449 self._assert_input_compatibility(inputs)
--> 450 outputs = self.call(inputs, *args, **kwargs)
451
452 # Apply activity regularization.
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/rnn_cell_impl.py in call(self, inputs, state)
936 [-1, cell.state_size])
937 cur_state_pos += cell.state_size
--> 938 cur_inp, new_state = cell(cur_inp, cur_state)
939 new_states.append(new_state)
940
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/rnn_cell_impl.py in __call__(self, inputs, state, scope)
772 self._recurrent_input_noise,
773 self._input_keep_prob)
--> 774 output, new_state = self._cell(inputs, state, scope)
775 if _should_dropout(self._state_keep_prob):
776 new_state = self._dropout(new_state, "state",
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/rnn_cell_impl.py in __call__(self, inputs, state, scope)
178 with vs.variable_scope(vs.get_variable_scope(),
179 custom_getter=self._rnn_get_variable):
--> 180 return super(RNNCell, self).__call__(inputs, state)
181
182 def _rnn_get_variable(self, getter, *args, **kwargs):
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/layers/base.py in __call__(self, inputs, *args, **kwargs)
448 # Check input assumptions set after layer building, e.g. input shape.
449 self._assert_input_compatibility(inputs)
--> 450 outputs = self.call(inputs, *args, **kwargs)
451
452 # Apply activity regularization.
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/rnn_cell_impl.py in call(self, inputs, state)
405
406 new_c = (
--> 407 c * sigmoid(f + self._forget_bias) + sigmoid(i) * self._activation(j))
408 new_h = self._activation(new_c) * sigmoid(o)
409
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
863 else:
864 raise
--> 865 return func(x, y, name=name)
866
867 def binary_op_wrapper_sparse(sp_x, y):
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py in _mul_dispatch(x, y, name)
1086 is_tensor_y = isinstance(y, ops.Tensor)
1087 if is_tensor_y:
-> 1088 return gen_math_ops._mul(x, y, name=name)
1089 else:
1090 assert isinstance(y, sparse_tensor.SparseTensor) # Case: Dense * Sparse.
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py in _mul(x, y, name)
1447 A `Tensor`. Has the same type as `x`.
1448 """
-> 1449 result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
1450 return result
1451
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
765 op = g.create_op(op_type_name, inputs, output_types, name=scope,
766 input_types=input_types, attrs=attr_protos,
--> 767 op_def=op_def)
768 if output_structure:
769 outputs = op.outputs
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
2630 original_op=self._default_original_op, op_def=op_def)
2631 if compute_shapes:
-> 2632 set_shapes_for_outputs(ret)
2633 self._add_op(ret)
2634 self._record_op_seen_by_control_dependencies(ret)
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in set_shapes_for_outputs(op)
1909 shape_func = _call_cpp_shape_fn_and_require_op
1910
-> 1911 shapes = shape_func(op)
1912 if shapes is None:
1913 raise RuntimeError(
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in call_with_requiring(op)
1859
1860 def call_with_requiring(op):
-> 1861 return call_cpp_shape_fn(op, require_shape_fn=True)
1862
1863 _call_cpp_shape_fn_and_require_op = call_with_requiring
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py in call_cpp_shape_fn(op, require_shape_fn)
593 res = _call_cpp_shape_fn_impl(op, input_tensors_needed,
594 input_tensors_as_shapes_needed,
--> 595 require_shape_fn)
596 if not isinstance(res, dict):
597 # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).
~/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
657 missing_shape_fn = True
658 else:
--> 659 raise ValueError(err.message)
660
661 if missing_shape_fn:
ValueError: Dimensions must be equal, but are 512 and 256 for 'decoding/decoder/while/BasicDecoderStep/decoder/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/mul' (op: 'Mul') with input shapes: [?,512], [?,256].
I am not able to understand the error. Which matrix is it trying to refer? Please help me, I am fairly new to Tensorflow.
The error says that inside the LSTM of the decoder (decoding/decoder/while/BasicDecoderStep/decoder/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/mul) there is a dimension mismatch during a multiplication (Mul).
My guess is that, for your implementation, you need twice as many cells for the decoder LSTM as for the encoder LSTM, due to the fact that you are using a bidirectional encoder. If you have a bidirectional encoder with a LSTM with 256 cells, then the result will have 512 units (as you concatenate the outputs of the forward and backward LSTM). Currently the decoder seems to expect an input of 256 cells.

Keras. Concatenate layers. TypeError

I want to create a siamese model, defined lower for colloborative filtration. First one creates users' embeddings, second one creates items' embeddings.
import keras
from keras import backend as K
from keras.layers import Input, Embedding, Dense, Flatten, concatenate
from keras.models import Model
n_users, n_items = 100, 3000
users_input = Input(shape=(n_users,), dtype='int32', name='users')
users_embedding = Embedding(output_dim=6, input_dim=n_users, input_length=1)(users_input)
users_flatten = Flatten()(users_embedding)
items_input = Input(shape=(n_items,), dtype='int32', name='items')
items_embedding = Embedding(output_dim=6, input_dim=n_items, input_length=1)(items_input)
items_flatten = Flatten()(items_embedding)
layer_0 = concatenate([users_flatten, items_flatten])
layer_1 = Dense(8, activation='relu')(layer_0)
layer_2 = Dense(1, activation='relu')(layer_1)
model = Model(inputs=[users_input, items_input], outputs=[layer_2])
As you see, I have problems with concatenation. Here is my stack trace:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-bf475de5f9cc> in <module>()
----> 1 layer_0 = concatenate([users_flatten, items_flatten])
2 layer_1 = Dense(8, activation='relu')(layer_0)
3 layer_2 = Dense(1, activation='relu')(layer_1)
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/layers/merge.pyc in concatenate(inputs, axis, **kwargs)
506 A tensor, the concatenation of the inputs alongside axis `axis`.
507 """
--> 508 return Concatenate(axis=axis, **kwargs)(inputs)
509
510
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
583
584 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 585 output = self.call(inputs, **kwargs)
586 output_mask = self.compute_mask(inputs, previous_mask)
587
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/layers/merge.pyc in call(self, inputs)
281 raise ValueError('A `Concatenate` layer should be called '
282 'on a list of inputs.')
--> 283 return K.concatenate(inputs, axis=self.axis)
284
285 def compute_output_shape(self, input_shape):
/home/vladimir/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in concatenate(tensors, axis)
1679 return tf.sparse_concat(axis, tensors)
1680 else:
-> 1681 return tf.concat([to_dense(x) for x in tensors], axis)
1682
1683
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.pyc in concat(concat_dim, values, name)
998 ops.convert_to_tensor(concat_dim,
999 name="concat_dim",
-> 1000 dtype=dtypes.int32).get_shape(
1001 ).assert_is_compatible_with(tensor_shape.scalar())
1002 return identity(values[0], name=scope)
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
667
668 if ret is None:
--> 669 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
670
671 if ret is NotImplemented:
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
174 as_ref=False):
175 _ = as_ref
--> 176 return constant(v, dtype=dtype, name=name)
177
178
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.pyc in constant(value, dtype, shape, name, verify_shape)
163 tensor_value = attr_value_pb2.AttrValue()
164 tensor_value.tensor.CopyFrom(
--> 165 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
166 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
167 const_tensor = g.create_op(
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape, verify_shape)
365 nparray = np.empty(shape, dtype=np_dt)
366 else:
--> 367 _AssertCompatible(values, dtype)
368 nparray = np.array(values, dtype=np_dt)
369 # check to them.
/home/vladimir/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in _AssertCompatible(values, dtype)
300 else:
301 raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 302 (dtype.name, repr(mismatch), type(mismatch).__name__))
303
304
TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
As an example I used Keras documenation for functional API. I use TensorFlow as backend.
Solution: update Keras and Tresorflow to the last versions.

Categories

Resources