在学习单层神经网络时使用from_tensor_slices,发生错误
报错ValueError: Unbatching a tensor is only supported for rank >= 1
import tensorflow as tf
from sklearn import datasets
from pandas import DataFrame
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
x_data = datasets.load_iris().data
y_data = datasets.load_iris().target
np.random.seed(116)
np.random.shuffle(x_data)
np.random.seed(116)
np.random.shuffle(y_data)
tf.random.set_seed(116)
x_train = x_data[:-30]
y_train = y_data[:-30]
x_test = x_data[-30:]
y_test = y_data[-30]
x_train = tf.cast(x_train, dtype=tf.float32)
x_test = tf.cast(x_test, dtype=tf.float32)
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)
test_data = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
运行结果及详细报错内容
在这一行:
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)
报错ValueError: Unbatching a tensor is only supported for rank >= 1