简体   繁体   English

Tensorflow 错误“TypeError:传递给 'Pack' Op 的 'values' 的列表中的张量具有不完全匹配的类型 [int32, int64, int32, int32, int32]。”

[英]Tensorflow Error “TypeError: Tensors in list passed to 'values' of 'Pack' Op have types [int32, int64, int32, int32, int32] that don't all match.”

I am new to Tensorflow and Keras.我是 Tensorflow 和 Keras 的新手。 I have loaded a dataset from CSV and created a train_dataset as such:我从 CSV 加载了一个数据集,并创建了一个 train_dataset:

column_names = ['a', 'date', 'c', 'd', 'e', 'f']
label_name = column_names[0]
feature_names = column_names[1:]
class_names = ['good', 'bad']

train_dataset = tf.data.experimental.make_csv_dataset(
    train_dataset_fp,
    batch_size,
    column_names=column_names,
    label_name=label_name,
    num_epochs=1)

features, labels = next(iter(train_dataset))
print(features)

My features are an OrderedDict and print as:我的功能是 OrderedDict 并打印为:

OrderedDict([('b', <tf.Tensor: shape=(32,), dtype=int32, numpy= array([1, 1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1], dtype=int32)>), ('date', <tf.Tensor: shape=(32,), dtype=int64, numpy= array([-9223372036855, 1262478794000, 1262426153000, 1262431717000, 1262425334000, 1262588520000, 1262425515000, 1262418072000, 1262420797000, 1262428601000, 1262590037000, 1262421322000, 1262433023000, 1262390762000, 1262590200000, 1262432769000, 1262427397000, -9223372036855, 1262425996000, 1262430050000, 1262431867000, 1262424427000, 1262420906000, 1262391208000, 1262590114000, -9223372036855, 1262589645000, 1262424306000, 1262428178000, 1262421300000, 1262423456000, 1262515569000])>), ('d', <tf.Tensor: shape=(32,), dtype=int32, numpy= array([357, 313, 557, 691, 292, 557, 605, 605, 48, 295, 81, 656, 321, 734, 584, 652, 575, 465, 71, 453, 196, 48, 689, 591, 676, 271, 67, 229, 740, 713, 230, 664], dtype=int32)>), ('e', <tf.Tensor: shape=(32,), dtype=i OrderedDict([('b', <tf.Tensor: shape=(32,), dtype=int32, numpy= array([1, 1, 0, 0, 0, 1, 0, 1, 0, 2, 1 , 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1], dtype=int32)>) , ('date', <tf.Tensor: shape=(32,), dtype=int64, numpy= array([-9223372036855, 1262478794000, 1262426153000, 1262431717000, 1262425334000, 1262588520000, 1262425515000, 1262418072000, 1262420797000, 1262428601000, 1262590037000, 1262421322000, 1262433023000, 1262390762000, 1262590200000, 1262432769000, 1262427397000, -9223372036855, 1262425996000, 1262430050000, 1262431867000, 1262424427000, 1262420906000, 1262391208000, 1262590114000, -9223372036855, 1262589645000, 1262424306000, 1262428178000, 1262421300000, 1262423456000, 1262515569000])>), (' d', <tf.Tensor: shape=(32,), dtype=int32, numpy= array([357, 313, 557, 691, 292, 557, 605, 605, 48, 295, 81, 656, 321, 734, 584, 652, 575, 465, 71, 453, 196, 48, 689, 591, 676, 271, 67, 229, 740, 713, 230, 664], dtype=int32)>), ('e' , <tf.Tensor: shape=(32,), dtype=i nt32, numpy= array([519, 537, 610, 178, 552, 610, 240, 240, 343, 643, 481, 340, 362, 143, 511, 167, 5, 685, 436, 105, 659, 343, 427, 242, 30, 717, 531, 492, 433, 452, 645, 303], dtype=int32)>), ('f', <tf.Tensor: shape=(32,), dtype=int32, numpy= array([ 345, 545, 1663, 1426, 2065, 1017, 1655, 47, 2070, -1, 1191, 191, 1569, 547, 1295, 1776, 1620, 680, 1990, 1642, 1930, 1465, 1887, 2128, 999, 447, 844, 1851, 1586, 1742, 2079, 729], dtype=int32)>)]) nt32, numpy= 数组([519, 537, 610, 178, 552, 610, 240, 240, 343, 643, 481, 340, 362, 143, 511, 167, 5, 685, 436, 105, 659, 343 , 427, 242, 30, 717, 531, 492, 433, 452, 645, 303], dtype=int32)>), ('f', <tf.Tensor: shape=(32,), dtype=int32, numpy=数组([345、545、1663、1426、2065、1017、1655、47、2070、-1、1191、191、1569、547、1295、1776、1620、680、1990、1642、5、1930、14 1887, 2128, 999, 447, 844, 1851, 1586, 1742, 2079, 729], dtype=int32)>)])

As you can see one of them has dtype=int64.如您所见,其中一个具有 dtype=int64。 I then use the following function to pack the features into an array:然后我使用以下 function 将特征打包到一个数组中:

def pack_features_vector(features, labels):
  features = tf.stack(list(features.values()), axis=1)
  return features, labels

However when I run it:但是,当我运行它时:

train_dataset = train_dataset.map(pack_features_vector)

I get the following error:我收到以下错误:

"TypeError: Tensors in list passed to 'values' of 'Pack' Op have types [int32, int64, int32, int32, int32] that don't all match." “TypeError:传递给'Pack' Op 的'values'的列表中的张量具有不完全匹配的类型[int32,int64,int32,int32,int32]。”

I understand that the issue is the stack function.我知道问题出在堆栈 function 上。 I have an epoch format date as my second feature which was read in as int64.我有一个纪元格式日期作为我的第二个功能,它被读取为 int64。 I think it may be easiest to convert all tensors to the same dType but I am not sure how.我认为将所有张量转换为相同的 dType 可能是最简单的,但我不确定如何。 I can see that features collection is an OrderedDict of Numpy arrays but I do not know how to change dType of the items.我可以看到功能集合是 Numpy arrays 的 OrderedDict 但我不知道如何更改项目的 dType。 I tried the following, it did not yeild a traceback but when I printed my features again all dtypes were still the same:我尝试了以下方法,它没有产生回溯,但是当我再次打印我的功能时,所有 dtypes 仍然相同:

for k,v in train_dataset:
  tf.dtypes.cast(v, tf.int64)

I would greatly appreciate any help.我将不胜感激任何帮助。 Thank you.谢谢你。

I think I have figured it out.我想我已经想通了。 I needed to add column_defaults:我需要添加 column_defaults:

column_names = ['a', 'b', 'date', 'd', 'e', 'f']
column_defaults=[tf.int64, tf.int64, tf.int64, tf.int64, tf.int64, tf.int64]
label_name = column_names[0]
feature_names = column_names[1:]
class_names = ['good', 'bad']

batch_size = 32

train_dataset = tf.data.experimental.make_csv_dataset(
    train_dataset_fp,
    batch_size,
    column_names=column_names,
    column_defaults=column_defaults,
    label_name=label_name,
    num_epochs=1)

features, labels = next(iter(train_dataset))

def pack_features_vector(features, labels):
  features = tf.stack(list(features.values()), axis=1)
  return features, labels

train_dataset = train_dataset.map(pack_features_vector)

But I am now trying to create a model and predictions:但我现在正在尝试创建 model 和预测:

model = tf.keras.Sequential([
  tf.keras.layers.Dense(128, activation=tf.nn.relu, input_shape=(5,)),
  tf.keras.layers.Dense(128, activation=tf.nn.relu),
  tf.keras.layers.Dense(2)
])

predictions = model(features)

And get "KeyError: 'dense_input'", not sure why.并得到“KeyError:'dense_input'”,不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:DataType float32 for attr&#39;Tindices&#39;不在允许值列表中:int32,int64 - TypeError:DataType float32 for attr 'Tindices' not in list of allowed values: int32, int64 将所有列从 int64 转换为 int32 - Convert all columns from int64 to int32 attr&#39;T&#39;的数据类型float32不在允许值列表中:int32,int64 - DataType float32 for attr 'T' not in list of allowed values: int32, int64 MongoDB - 将字段从 int32 转换为 int64 - MongoDB - Converting fields from int32 to int64 将 dtype 从 int64 转换为 int32 - Convert dtype from int64 to int32 使用int64标量切片int32形状的张量 - Slicing tensor with int32 shape with int64 scalar pytorch int32 到 int64 转换 - pytorch int32 to int64 conversion 在 pandas.to_numeric() 中将 int64 转换为 int32 - convert int64 to int32 in pandas.to_numeric() Tensorflow类型错误:传递给参数&#39;shape&#39;的值具有DataType float32不在允许值列表中:int32,int64 - Tensorflow Type Error: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64 了解 Keras 错误:TypeError:传递给参数“shape”的值的 DataType float32 不在允许值列表中:int32、int64 - Understanding a Keras Error: TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM