本人小白,并没有研究底层代码,虚心求教大佬们这个问题的原因
今天在使用虚拟环境导入keras的时候碰到了一些导包的版本问题,遂记录一下,
tensorflow2.10也是一样的情况,可能也适用于其他版本tensorflow。
conda activate wind
pip install tensorflow
自动安装了2.13
版本的tensorflow
,同时pip也会自动下载keras<2.14,>=2.13.1
.
测试1:
from tensorflow.python.keras.layers import Dense, Input
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.activations import sigmoid
按理来说这种导包应该是正确的,因为查看目录发现tensorflow
目录下keras确实是这个结构,不知道为什么会报错,并且解决了一个错误会冒出另一个错误。。。
比如:AttributeError: module 'tensorflow.python.distribute.input_lib' has no attribute 'DistributedDatasetInterface'
测试2:
# 用这个爆红不报错
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras import Sequential
from tensorflow.keras.activations import sigmoid
用这个导入,会爆红,但是代码可以正常运行,
测试3,完美解决,
直接导入下载tensorflow包时自动下载的keras包
#用这个完美解决问题
from keras.layers import Dense, Input
from keras import Sequential
from keras.activations import sigmoid
用来重现问题的示例
import numpy as np
import tensorflow as tf
import logging
# from tensorflow.python.keras.layers import Dense, Input
# from tensorflow.python.keras import Sequential
# from tensorflow.python.keras.activations import sigmoid
# from tensorflow.keras.layers import Dense, Input
# from tensorflow.keras import Sequential
# from tensorflow.keras.activations import sigmoid
# from keras.layers import Dense, Input
# from keras import Sequential
# from keras.activations import sigmoid
logging.getLogger("tensorflow").setLevel(logging.ERROR)
tf.autograph.set_verbosity(0)
X_train = np.array([0., 1, 2, 3, 4, 5], dtype=np.float32).reshape(-1,1)
Y_train = np.array([0, 0, 0, 1, 1, 1], dtype=np.float32).reshape(-1,1)
model = Sequential([
Input(shape=(1,)),
Dense(1, activation=sigmoid, name = "L1")
])
model.summary()
# Setting the weights and bias of the neuron
logistic_layer = model.get_layer('L1')
set_w = np.array([[2]])
set_b = np.array([-4.5])
logistic_layer.set_weights([set_w, set_b])
# Performance test
a1 = model.predict(X_train[0].reshape(1,1)) # this line caused the error
print(a1)
alog = sigmoid(np.dot(set_w,X_train[0].reshape(1,1)) + set_b)
print(alog)
参考外网解决方案,基本可以确定这是一个tensorflow更新换代但是没完全更新换代的遗留问题,具体原因希望后来的大佬看到了帮忙解答一下或者贴出链接!
DistributedDatasetInterface is not an attribute of input_lib - engine\data_adapter.py #61215