Keras和tensorflow1.15
1.关于 Keras 的 import 方式
from keras import backend as k
from keras.layers import Input, Dense, Lambda
报错:ImportError: cannot import name ‘get_config’ from 'tensorflow.python.eager.context’
总之:就是要在原来的 import 方式的基础上 添加 tensorflow,这和 tf 2.0比较像…
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Input, Dense, Lambda
from tensorflow.keras.models import Model
2.关于 tensorflow1.15
2.1创建常量 tensor
2.1.1Tensorflow提供 constant 这个函数:
constant(value, dtype=None, shape=None, name="Const", verify_shape=False)
零维张量
t = tf.constant(1, dtype=tf.float32)
一维张量
t = tf.constant([1, 2, 3, 4], dtype=tf.float32)
二维张量
t = tf.constant(
[
[1, 2, 3, 4],
[7, 8, 9, 10]
], dtype=tf.float32
)
三维及以上 以此类推
Note: 在tensorflow v1 版本中,不能直接 print(t),这与python、pytorch、tensorflow2.0及以上中的变量不同,tensorflow v1版本中创建的变量不是 立即执行 的, 需要一个Session对象,只有执行了Session对象才能得到最终的结果
如图:
执行 Session 后(其中:sess = tf.Session())
3.Keras 和 tensorflow1.15的联合使用
import tensorflow as tf
from tf.keras import backend as k
from tf.keras.layers import Input
# keras 的 Input 用法类似于 tf 的 placeholder占位符
x = Input(shape=(4, ), dtype=tf.float32)
result = k.sum(x)
with tf.Session() as sess:
sess.run(result, feed_dict={x:tf.constant([[1, 2, 3, 4]], dtype=tf.float32)})
运行会报错:TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. For reference, the tensor object was Tensor(“Const_7:0”, shape=(1, 4), dtype=float32) which was passed to the feed with key Tensor(“input_1:0”, shape=(?, 4), dtype=float32).
报错提示比较明显:就是喂给训练操作的数据不能是张量,只能是 python的scalars, strings, lists, numpy ndarrays, or TensorHandles
Note:TensorHandles—>将张量操作算出来的结果,
因为 tensorflow v1 版本的变量不是立即执行的,不执行 Session().run() 是没有结果的。
当传参为:python的 list 时:
import tensorflow as tf
from tf.keras import backend as k
from tf.keras.layers import Input
# keras 的 Input 用法类似于 tf 的 placeholder占位符
x = Input(shape=(4, ), dtype=tf.float32)
result = k.sum(x)
with tf.Session() as sess:
sess.run(result, feed_dict={x:[[1, 2, 3, 4]]})
执行结果为:
当传参为:numpy 的 array 时:
import numpy as np
import tensorflow as tf
from tf.keras import backend as k
from tf.keras.layers import Input
# keras 的 Input 用法类似于 tf 的 placeholder占位符
x = Input(shape=(4, ), dtype=tf.float32)
result = k.sum(x)
with tf.Session() as sess:
sess.run(result, feed_dict={x:np.array([[1, 2, 3, 4]])})
执行结果为:
当传参为:TensorHandles 时:
import tensorflow as tf
from tf.keras import backend as k
from tf.keras.layers import Input
# keras 的 Input 用法类似于 tf 的 placeholder占位符
x = Input(shape=(4, ), dtype=tf.float32)
result = k.sum(x)
with tf.Session() as sess:
sess.run(result, feed_dict={x:tf.constant([[1, 2, 3, 4]], dtype=tf.float32).eval(session=sess)})
Note: 在喂给训练操作的张量后面加 .eval(),将张量操作算出来的结果喂给训练操作就正常了
执行结果为:
10.0
Note: 在 with 结构的上下文管理器中定义 tf.Session() 时,eval()的session参数可以传,也可以不传。
但是,当 tf.Session() 不是通过 with 结构的上下文管理器,即 sess = tf.Session() 时,eval()的 session参数必须传递,否则会报错,没有默认的 session。
并且最后还要执行,sess.close(), 即关闭 session.
遇到这种情况:将 eval()—>eval(session=sess) 即可解决。
Reference:
2 Tensorflow - 标量运算
TensorFlow 报错 TypeError: The value of a feed cannot be a tf.Tensor object
2、介绍在TensorFlow当中使用不同的方式创建张量tensor