首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow:无法为生成TypeSpec

Tensorflow:无法为生成TypeSpec
EN

Stack Overflow用户
提问于 2020-11-24 22:38:37
回答 1查看 3.4K关注 0票数 2

我建立了一个模型,试图在-1到1的区间内预测句子的情绪。我想将标量(1个神经元)输出更改为3个神经元%的输出( 0%负,100%中性,0%正)。为此,我通过以下方式更改了我的Y值:

代码语言:javascript
复制
def encodeSentiment(sentiment):
    if sentiment == "negative":
        return tf.one_hot(0, 3)
    if sentiment == "neutral":
        return tf.one_hot(1, 3)
    if sentiment == "positive":
        return tf.one_hot(2, 3)
    assert False

这样做之后,我的Y看起来像这样:

我刚刚将输出图层从tf.keras.layers.Dense(1, activation='tanh')更改为tf.keras.layers.Dense(3, activation='softmax')

代码语言:javascript
复制
model = tf.keras.Sequential([
    encoder,
    tf.keras.layers.Embedding(len(encoder.get_vocabulary()), 128, mask_zero=True),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128,  return_sequences=True)),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(3, activation='softmax')
])

但是当我尝试训练网络时,我得到了以下错误:

代码语言:javascript
复制
TypeError: Could not build a TypeSpec for 0        (tf.Tensor(0.0, shape=(), dtype=float32), tf.T...
1        (tf.Tensor(1.0, shape=(), dtype=float32), tf.T...
2        (tf.Tensor(1.0, shape=(), dtype=float32), tf.T...
3        (tf.Tensor(1.0, shape=(), dtype=float32), tf.T...
4        (tf.Tensor(1.0, shape=(), dtype=float32), tf.T...
                               ...                        
27476    (tf.Tensor(1.0, shape=(), dtype=float32), tf.T...
27477    (tf.Tensor(1.0, shape=(), dtype=float32), tf.T...
27478    (tf.Tensor(0.0, shape=(), dtype=float32), tf.T...
27479    (tf.Tensor(0.0, shape=(), dtype=float32), tf.T...
27480    (tf.Tensor(0.0, shape=(), dtype=float32), tf.T...
Name: sentiment_encode, Length: 27481, dtype: object with type Series

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-105-b16132770690> in <module>
     19                     validation_steps=30,
     20                     verbose=2, #output 1x per epoch
---> 21                     callbacks=[earlyStopping])

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
    106   def _method_wrapper(self, *args, **kwargs):
    107     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
--> 108       return method(self, *args, **kwargs)
    109 
    110     # Running inside `run_distribute_coordinator` already.

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\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_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1061           use_multiprocessing=use_multiprocessing,
   1062           model=self,
-> 1063           steps_per_execution=self._steps_per_execution)
   1064 
   1065       # Container that configures and calls `tf.keras.Callback`s.

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
   1115         use_multiprocessing=use_multiprocessing,
   1116         distribution_strategy=ds_context.get_strategy(),
-> 1117         model=model)
   1118 
   1119     strategy = ds_context.get_strategy()

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
    362     indices_dataset = indices_dataset.flat_map(slice_batch_indices)
    363 
--> 364     dataset = self.slice_inputs(indices_dataset, inputs)
    365 
    366     if shuffle == "batch":

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\data_adapter.py in slice_inputs(self, indices_dataset, inputs)
    388     dataset = dataset_ops.DatasetV2.zip((
    389         indices_dataset,
--> 390         dataset_ops.DatasetV2.from_tensors(inputs).repeat()
    391     ))
    392 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\data\ops\dataset_ops.py in from_tensors(tensors)
    602       Dataset: A `Dataset`.
    603     """
--> 604     return TensorDataset(tensors)
    605 
    606   @staticmethod

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\data\ops\dataset_ops.py in __init__(self, element)
   2980   def __init__(self, element):
   2981     """See `Dataset.from_tensors()` for details."""
-> 2982     element = structure.normalize_element(element)
   2983     self._structure = structure.type_spec_from_value(element)
   2984     self._tensors = structure.to_tensor_list(self._structure, element)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\data\util\structure.py in normalize_element(element)
     96         # the value. As a fallback try converting the value to a tensor.
     97         normalized_components.append(
---> 98             ops.convert_to_tensor(t, name="component_%d" % i))
     99       else:
    100         if isinstance(spec, sparse_tensor.SparseTensorSpec):

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1497 
   1498     if ret is None:
-> 1499       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1500 
   1501     if ret is NotImplemented:

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    336                                          as_ref=False):
    337   _ = as_ref
--> 338   return constant(v, dtype=dtype, name=name)
    339 
    340 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
    262   """
    263   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 264                         allow_broadcast=True)
    265 
    266 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    273       with trace.Trace("tf.constant"):
    274         return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 275     return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    276 
    277   g = ops.get_default_graph()

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
    299   """Implementation of eager constant."""
--> 300   t = convert_to_eager_tensor(value, ctx, dtype)
    301   if shape is None:
    302     return t

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor).
代码语言:javascript
复制
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), 
            optimizer=tf.keras.optimizers.Adam(1e-4),
            metrics=['accuracy'])

history = model.fit(x=trainX, y=trainY,
                    validation_data=(valX, valY),
                    epochs=100, #1k
                    validation_steps=30,
                    verbose=2, #output 1x per epoch
                    callbacks=[earlyStopping])

我是Tensorflow的新手,你们能帮我找到解决这个问题的方法吗?

高级中的Ty

EN

回答 1

Stack Overflow用户

发布于 2020-11-25 18:32:45

看起来TrainY是一个列表。您的目标可以是Numpy数组或TensorFlow张量。它应该与x一致(你不能有Numpy输入和张量目标,或者相反)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64988725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档