hi各位大佬好,我是菜鳥小明哥,在寫代碼中tf1的代碼很多都不能直接轉為tf2,需要替換為同樣功能的函式,因此本篇暫且統計遇到的幾個及使用方法,以備后用,
For Recommendation in Deep learning QQ Group 277356808
For deep learning QQ Second Group 629530787
I'm here waiting for you
不接受這個網頁的私聊/私信!!!
歡迎關注微信視頻號、快手:小明哥直播間
1-dropout這個已經提及不再贅述,
2-全連接dense/MLP也已論述,
3-卷積conv如下:1d示例,
提起卷積肯定要說起輸入資料的格式,有NCW和NWC兩種格式(舊的4個字母的已經廢棄),其實就是tf和torch的區別,分別為:就是首字母唄,
NWC:[batch, in_width, in_channels]
NCW:[batch, in_channels, in_width]
tf1中tf.nn.conv1d
conv1d(value=None, filters=None, stride=None, padding=None, use_cudnn_on_gpu=None, data_fo
rmat=None, name=None, input=None, dilations=None)
Computes a 1-D convolution given 3-D input and filter tensors. (deprecated argument va
lues) (deprecated argument values)
tf1中tf.layers.conv1d
conv1d(inputs, filters, kernel_size, strides=1, padding='valid', data_format='channels_last', dilation_rate=1, activation=None, use_bias=True, kernel_initializer=None, bias_initializer=<tensorflow.python.ops.init_ops.Zeros object at 0x7fd5cbcc3650>, kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, trainable=True, name=None, reuse=None)
tf2
class Conv1D(Conv)
| Conv1D(filters, kernel_size, strides=1, padding='valid', data_format='channels_last', dilation_rate=1, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None, **kwargs)
從上面的對比中發現tf1中第一個的缺少kernel_size,下面嘗試計算下:舉例如下
發現第一個需要指定padding的模式,否則出錯,
后兩個的out shape 是一樣的,但在tf1環境下報錯,真是夠了,tf2下無錯,
inputs=tf.random.normal((4,30,18))
>>> filter=16
>>> kernel_size=1
tf1中
out1=tf.layers.conv1d(inputs,filter,kernel_size)
/tensor_shape.py", line 193, in __init__
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'type'
>>> conv=tf.compat.v2.keras.layers.Conv1D(filter,kernel_size)
>>> out2=conv(inputs)
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'type'
tf2中
>>> out1=tf.compat.v1.layers.conv1d(inputs,filter,kernel_size)
>>> conv=tf.compat.v2.keras.layers.Conv1D(filter,kernel_size)
>>> out2=conv(inputs)
>>> out1.shape
TensorShape([4, 30, 16])
>>> out2.shape
TensorShape([4, 30, 16])
愿我們終有重逢之時,而你還記得我們曾經討論的話題,
4-數學計算,這種加上math即可
>>> tf.log
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'log'
>>> tf.math.log(2.78)
<tf.Tensor: shape=(), dtype=float32, numpy=1.0224509>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/379229.html
標籤:AI
上一篇:IMDB電影資料分析實踐
下一篇:將一個模式的值推入另一個模式
