Google Colab TensorFlow 'DistributedDatasetInterface'错误

ryoqjall  于 11个月前  发布在  Go
关注(0)|答案(1)|浏览(229)

找到解决办法了。
卸载最新版本的tensorflow并恢复到以前的模型

# Uninstall the current version of TensorFlow.
!pip uninstall tensorflow -2.13.0

# Install TensorFlow 1.15.0.
!pip install tensorflow==2.12.0

# Verify the TensorFlow version.
import tensorflow as tf
print(tf.__version__)

字符串
使用Google Colab。这是我运行model.fit(x,y)时的错误。

AttributeError:模块'tensorflow.python.distribute.input_lib'没有属性'DistributedDatasetInterface'

我读到它涉及到'keras'和'tensorflow'之间的兼容性问题,解决的方法是在谷歌colab中更新tensor flow,

!pip install -U tensorflow


我这样做了,我的tensorflow版本现在是2.13.0,但这并没有解决这个问题。
这些是我的图书馆

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.activations import linear
from tensorflow.python.keras.optimizers import adam_v2


序列
1.加载csv文件,提取数据并重新整形。
1.使用sklearn train_test_split()创建训练、验证和测试集
1.使用**tf.keras.layers.Normalization(axis=-1,mean=None,variance=None)**对所有x_train,x_validation,x_test数据进行归一化
1.运行模型

[
        tf.keras.layers.Dense(input_shape=(30,), units=15, activation='relu',
                              name='L1'),
        tf.keras.layers.Dense(input_shape=(15,), units=10, activation='relu',
                              name='L2'),
        tf.keras.layers.Dense(input_shape=(10,), units=1, activation='linear',
                              name='L3'),
    ]  , name="My_Model"
)

opt = adam_v2.Adam(learning_rate=0.001)
loss = tf.keras.losses.MeanSquaredError()
model.compile(
    loss = loss,
    optimizer = opt,
)

model.fit(xn_train, y_train, epochs=100)


这是有错误的**model.fit()**部分。
a.所以我确保输入(xn_train,y_train)都是<class 'numpy. ndarray'>。
B.然后确保,但输入都是<class 'tensorflow.python.framework.ops. penserTensor'>。这也没有修复它
请帮助解决这个问题。

stszievb

stszievb1#

tensorflow/python/keras是遗留代码。请删除它并使用tf.keras。
下面的例子对我来说很有用。

import tensorflow as tf

Input = tf.keras.layers.Input
Dense = tf.keras.layers.Dense
Activation = tf.keras.layers.Activation
Dropout = tf.keras.layers.Dropout
Model = tf.keras.models.Model

input_layer = Input(shape=(X.shape[1],))
dense_layer_1 = Dense(15, activation='relu')(input_layer)
dense_layer_2 = Dense(10, activation='relu')(dense_layer_1)
output = Dense(y.shape[1], activation='softmax')(dense_layer_2)

model = Model(inputs=input_layer, outputs=output)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics='acc'])

print(model.summary())

history = model.fit(X_train, y_train, batch_size=8, epochs=50, verbose=1, validation_split=0.2)

字符串

import tensorflow as tf
from keras.layers import Input, Dense
from keras.models import Model

input_layer = Input(shape=(X.shape[1],))
dense_layer_1 = Dense(15, activation='relu')(input_layer)
dense_layer_2 = Dense(10, activation='relu')(dense_layer_1)
output = Dense(y.shape[1], activation='softmax')(dense_layer_2)

model = Model(inputs=input_layer, outputs=output)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])

print(model.summary())

history = model.fit(X_train, y_train, batch_size=8, epochs=50, verbose=1, validation_split=0.2)

相关问题