2.13版本的tensorflow如何使用keras导包

本人小白,并没有研究底层代码,虚心求教大佬们这个问题的原因
今天在使用虚拟环境导入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
在这里插入图片描述

AttributeError: module ‘tensorflow.python.distribute.input_lib’ has no attribute ‘DistributedDatasetInterface’
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值