Simon Shi的小站

人工智能,机器学习, 强化学习,大模型,自动驾驶

0%

Tensorflow 2.x

tensorflow2

tf keras python 依赖
TF2.0 Keras2.3.1 3.6,3.7
TF2.1 Keras2.3.1 3.6,3.7
TF2.2 Keras2.3.1 3.6-3.8
TF2.3 Keras2.4.3 3.6-3.8 h5py=2.10.0
hdf5=1.10.4
numpy=1.19.2
TF2.4 Keras2.4.3 3.6-3.8
TF2.5 3.6-3.9
TF2.6 Keras2.6 3.6-3.9

ENV

CUDA11.1+cuDNN8.0.5+tensorflow-gpu2.4.1

CUDA11.1+cuDNN8.0.5+tensorflow-gpu2.4.1

CUDA11.1+cuDNN8.0.5+tensorflow-gpu2.4.1

load

1
2
3
tf.saved_model.load(
export_dir, tags=None, options=None
)

Loading Keras models

1
2
3
4
model = tf.keras.Model(...)
tf.saved_model.save(model, path)
imported = tf.saved_model.load(path)
outputs = imported(inputs)

tf.gather tf.gather_nd

tf.gather和tf.gather_nd都是从tensor中取出index标注的部分,不同之处在于,gather一般只使用一个index来标注,而gather_nd可以使用多个index。

1
2
3
4
5
6
7
8
9
10
11
12
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
# import tensorflow as tf
params = tf.constant([["a", "b"], ["c", "d"], ["e", "f"]])
gather = tf.constant([0, 2])
gather_nd = tf.constant([[0,0], [1, 1]])


with tf.Session() as sess:
print(tf.gather(params, gather).eval())
print()
print(tf.gather_nd(params, gather_nd).eval())
1
2
3
4
[[b'a' b'b']
[b'e' b'f']]

[b'a' b'd']

Keras

1
2
tf.keras.layers.Dense()
tf.keras.layers.LSTM()

LSTM

https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/keras/layers/LSTM

1
2
3
4
5
6
7
8
9
10
11
12
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape) #(32,4)

lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True)
whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
print(whole_seq_output.shape)#(32,10,4)

print(final_memory_state.shape) #(32,4)

print(final_carry_state.shape) #(32,4)

BUGS

1
2
解决报错“ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context'”
安装的tensorflow版本和keras版本可能不合适的时候,会出现报错
1
2
3
4
5
6
7
module 'tensorflow_core.compat.v2' has no attribute '__internal__'

from tensorflow import keras
from keras import layers
-->
from tensorflow import keras
from tensorflow.keras import layers
1
Cannot convert a symbolic Tensor (lstm/strided_slice:0) to a numpy array.
1
conda install -c anaconda h5py

LSTM+Dense报错

1
2
3
4
NotImplementedError: Cannot convert a symbolic Tensor 
(lstm/strided_slice:0) to a numpy array. This error
may indicate that you're trying to pass a Tensor to a NumPy call,
which is not supported

解决:安装 numpy==1.19.2,太高的numpy版本不行