【问题标题】:TensorFlow Error: dictionary update sequence element #0 has length 6; 2 is requiredTensorFlow 错误:字典更新序列元素 #0 的长度为 6; 2 是必需的
【发布时间】:2022-06-27 20:55:48
【问题描述】:

我是 Python 和机器学习的新手。每当我运行以下代码时都会出现错误:

def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
  def input_function():  
    ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))  
    if shuffle:
      ds = ds.shuffle(1000)  
    ds = ds.batch(batch_size).repeat(num_epochs) 
    return ds  
  return input_function  


train_input_fn = make_input_fn(X_train, y_train)  
eval_input_fn = make_input_fn(X_test, y_test, num_epochs=1, shuffle=False)

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)

linear_est.train(train_input_fn) 
result = linear_est.evaluate(eval_input_fn) 

clear_output() 
print(result['accuracy']) 

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-38-47fb35491976> in <module>()
----> 1 linear_est.train(train_input_fn)
      2 result = linear_est.evaluate(eval_input_fn)
      3 
      4 clear_output()
      5 print(result['accuracy'])

5 frames
<ipython-input-36-16cfc32eb7b2> in input_function()
      1 def make_input_fn(X_train, y_train, num_epochs=10, shuffle=True, batch_size=32):
      2   def input_function():
----> 3     ds = tf.data.Dataset.from_tensor_slices((dict(X_train), y_train))
      4     if shuffle:
      5       ds = ds.shuffle(1000)

ValueError: dictionary update sequence element #0 has length 6; 2 is required

我不确定问题是否与我的数据或数据类型有关。我的数据没有空格。

【问题讨论】:

    标签: python tensorflow machine-learning


    【解决方案1】:

    您收到此错误是因为您创建的字典格式不正确。

    例如:

    t1 = ('a','b','c','d')
    t2 = (1,2,3,4)
    dict(t1) #output: ValueError: dictionary update sequence element #0 has length 1; 2 is required
    dict(zip(t1,t2)) #output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    

    使用正确格式的dict 函数创建字典不会产生该错误。谢谢!

    【讨论】:

      猜你喜欢
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 2015-02-22
      相关资源
      最近更新 更多