【问题标题】:How to fix this errror 'failed to convert numpy array to a Tensor'如何修复此错误“无法将 numpy 数组转换为张量”
【发布时间】:2021-01-02 22:58:01
【问题描述】:

我正在使用年龄、性别等数据(总共 6 个值)在 tensorflow 上制作保险费预测器(给出单个输出,这是一个浮点数)。 这是代码

import tensorflow as tf
import numpy as np
from tensorflow import keras
import pandas as pd
a=0
file=pd.read_csv(r"""C:\Users\lavni\OneDrive\Desktop\proj.csv""",sep=',',index_col=False)
Data = file[['age', 'sex', 'bmi', 'children', 'smoker', 'region']].to_numpy()
Charges=file[['charges']].to_numpy()
model = keras.Sequential()
model.add(keras.layers.Dense(units=6, input_shape=(6,)))
model.compile(optimizer='sgd',loss='mean_squared_error')

model.fit(Data,Charges)

当我运行代码时,我得到以下错误:

  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\ML.py", line 14, in <module>
    model.fit(Data,Charges)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1063, in fit
    steps_per_execution=self._steps_per_execution)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1117, in __init__
    model=model)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 265, in __init__
    x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1021, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py", line 635, in map_structure
    structure[0], [func(*x) for x in entries],
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py", line 635, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1016, in _convert_numpy_and_scipy
    return ops.convert_to_tensor(x, dtype=dtype)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\ops.py", line 1499, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 264, in constant
    allow_broadcast=True)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 275, in _constant_impl
    return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 300, in _constant_eager_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "C:\Users\lavni\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\constant_op.py", line 98, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

感谢您的任何帮助,并提前感谢您。

【问题讨论】:

    标签: python python-3.x numpy tensorflow keras


    【解决方案1】:
    Charges = np.array([file.loc[:,'charges']])
    

    这可能会解决您的问题。首先,使用loc 方法过滤数据框。之后,将其转换为 numpy 数组。

    【讨论】:

    • 它给了我这个错误:ValueError: Data cardinality is ambiguous: x sizes: 1338 y sizes: 1 Please provide data which shares the same first dimension.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2021-11-26
    • 2021-10-19
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多