tensorflow: 激活函数(Activation_Functions) 探究

激活函数概念

From TensorFlow - Activation_Functions

在神经网络中,我们有很多的 非线性函数 来作为 激活函数

连续 、平滑

tf.sigmoid(x, name = None)

== 1 / (1 + exp(-x))

import numpy as np
import tensorflow as tf

sess = tf.Session()
bn = np.random.normal(0, 5, [3, 5])

print bn.shape, type(bn), ':'
print bn
print
output = tf.nn.sigmoid(bn)
print output.shape, type(output), ':'
print sess.run(output)
(3, 5) <type 'numpy.ndarray'> :
[[ 2.42429203 -1.89521415  4.52536321  2.02200042 -0.46109594]
 [-5.37984794  3.82258344  3.05039891  5.35911657  4.04462726]
 [-3.79266918 -7.12570837  1.74167827 -0.85649631 -3.77669239]]

(3, 5) <class 'tensorflow.python.framework.ops.Tensor'> :
[[  9.18661034e-01   1.30651098e-01   9.89285269e-01   8.83087698e-01
    3.86725869e-01]
 [  4.58738158e-03   9.78596887e-01   9.54799746e-01   9.95316973e-01
    9.82785304e-01]
 [  2.20387197e-02   8.03517003e-04   8.50900112e-01   2.98071887e-01
    2.23857102e-02]]


tf.tanh(x, name = None)

== ( exp(x) - exp(-x) ) / ( exp(x) + exp(-x) )

import numpy as np
import tensorflow as tf

sess = tf.Session()
bn = np.random.normal(0, 5, [3, 5])

print bn.shape, type(bn), ':'
print bn
print
output = tf.nn.tanh(bn)
print output.shape, type(output), ':'
print sess.run(output)
(3, 5) <type 'numpy.ndarray'> :
[[-1.43756487 -0.82183219  2.83650212 -0.86855883 -2.54894335]
 [ 2.3639829  -5.23813843  6.94823124 -6.59737671  3.62198313]
 [ 9.15073151  2.82883771 -4.40860502 -5.96409016 -2.74915937]]

(3, 5) <class 'tensorflow.python.framework.ops.Tensor'> :
[[-0.89320646 -0.67606587  0.99314851 -0.70064117 -0.98785491]
 [ 0.98246619 -0.99994361  0.99999816 -0.99999628  0.99857208]
 [ 0.99999998  0.99304304 -0.99970372 -0.9999868  -0.99184608]]


tf.nn.softplus(features, name = None)

== log ( exp( features ) + 1)

import numpy as np
import tensorflow as tf

sess = tf.Session()
bn = np.random.normal(0, 5, [3, 5])

print bn.shape, type(bn), ':'
print bn
print
output = tf.nn.softplus(bn)
print output.shape, type(output), ':'
print sess.run(output)
(3, 5) <type 'numpy.ndarray'> :
[[ 2.3897838  -9.86605463 -7.58004249 -4.38702367 -1.44367065]
 [ 7.52588384  6.49497224 -4.37733996 -0.68677868 -2.12110005]
 [-6.35464811 -1.70150615  6.51252343 -0.12833586  4.36898049]]

(3, 5) <class 'tensorflow.python.framework.ops.Tensor'> :
[[  2.47747365e+00   5.19057707e-05   5.10409248e-04   1.23609802e-02
    2.11928637e-01]
 [  7.52642265e+00   6.49648212e+00   1.24805143e-02   4.07592450e-01
    1.13239092e-01]
 [  1.73713721e-03   1.67553530e-01   6.51400705e+00   6.31036601e-01
    4.38156511e+00]]


连续、不平滑

tf.nn.relu(features, name = None)

== max (features, 0)

import numpy as np
import tensorflow as tf

sess = tf.Session()
bn = np.random.normal(0, 5, [3, 5])

print bn.shape, type(bn), ':'
print bn
print
output = tf.nn.relu(bn)
print output.shape, type(output), ':'
print sess.run(output)
(3, 5) <type 'numpy.ndarray'> :
[[ 4.34288636 -3.14906286 -5.21796011 -2.77006242 -4.92871322]
 [ 9.07049557 -9.64290379 -5.91523423  1.59385546 -2.04672855]
 [-4.10765782  1.51740207 -0.5572445   8.21818142 -4.67065521]]

(3, 5) <class 'tensorflow.python.framework.ops.Tensor'> :
[[ 4.34288636  0.          0.          0.          0.        ]
 [ 9.07049557  0.          0.          1.59385546  0.        ]
 [ 0.          1.51740207  0.          8.21818142  0.        ]]


tf.nn.relu6(features, name = None)

== min ( max(features, 0), 6 )

import numpy as np
import tensorflow as tf

sess = tf.Session()
bn = np.random.normal(0, 5, [3, 5])

print bn.shape, type(bn), ':'
print bn
print
output = tf.nn.relu6(bn)
print output.shape, type(output), ':'
print sess.run(output)
(3, 5) <type 'numpy.ndarray'> :
[[ 6.08205437  7.72360999 -1.62220085  5.41621866  5.8087728 ]
 [-5.07454654  3.85471614  1.44742944  2.77378759  3.61971044]
 [ 5.43383943  1.9598894  -2.5352505  -1.38550512  3.64028622]]

(3, 5) <class 'tensorflow.python.framework.ops.Tensor'> :
[[ 6.          6.          0.          5.41621866  5.8087728 ]
 [ 0.          3.85471614  1.44742944  2.77378759  3.61971044]
 [ 5.43383943  1.9598894   0.          0.          3.64028622]]


tf.nn.bias_add(value, bias, name = None)

== value + bias (bias是一维的)

import numpy as np
import tensorflow as tf

sess = tf.Session()
bn = np.random.normal(0, 5, [3, 5])

print bn.shape, type(bn), ':'
print bn
print
output = tf.nn.bias_add(value=bn, bias=np.ones_like(bn[0]))
print output.shape, type(output), ':'
print sess.run(output)
(3, 5) <type 'numpy.ndarray'> :
[[-7.24470546  1.40561024  2.27976912 -6.22879516  4.98934916]
 [-9.75160657  6.78796922  0.60843038 -4.94145474 -0.98402315]
 [-7.02590057  1.98236592  0.85727947  0.08917467 -5.54994355]]

(3, 5) <class 'tensorflow.python.framework.ops.Tensor'> :
[[-6.24470546  2.40561024  3.27976912 -5.22879516  5.98934916]
 [-8.75160657  7.78796922  1.60843038 -3.94145474  0.01597685]
 [-6.02590057  2.98236592  1.85727947  1.08917467 -4.54994355]]


随机正则化

tf.nn.dropout(x, keep_prob, noise_shape = None, seed = None, name = None)

== keep_prob概率 的神经元输出值将被放大到原来的 1/keep_prob 倍,其余神经元的输出置 0

import numpy as np
import tensorflow as tf

sess = tf.Session()
bn = np.random.normal(0, 5, [3, 5])

print bn.shape, type(bn), ':'
print bn
print
output = tf.nn.dropout(x=bn, keep_prob=0.5)
print output.shape, type(output), ':'
print sess.run(output)
(3, 5) <type 'numpy.ndarray'> :
[[ -6.63260663   5.18248388  -2.64777118  -0.98104194  -4.21568201]
 [  0.94315835   5.73277238  -0.27942206   0.93593509  10.41087634]
 [  0.18322279   5.72198372   5.00533604  -1.80672579  -2.32201658]]

(3, 5) <class 'tensorflow.python.framework.ops.Tensor'> :
[[ -0.           0.          -5.29554235  -1.96208389  -0.        ]
 [  1.88631669   0.          -0.55884411   1.87187017  20.82175267]
 [  0.          11.44396745   0.          -0.          -0.        ]]


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误通常是由于在实例化RNN层时,没有指定cell参数引起的。在tensorflow2.x版本中,RNN层已经被检查,需要明确指定cell参数。以下是一个创建简单LSTM模型的例子: ``` python import tensorflow as tf model = tf.keras.Sequential([ tf.keras.layers.LSTM(64, input_shape=(None, 10), return_sequences=True), tf.keras.layers.Dense(1) ]) ``` 在这个模型中,我们使用了一个LSTM层,输入的shape是(None, 10),输出的shape也是(None, 10),因为我们设置了return_sequences=True。如果不设置return_sequences=True,那么输出的shape将会是(None, 64)。在实例化LSTM层时,我们没有指定cell参数,因为LSTM层已经默认使用了LSTMCell。 如果你需要自定义RNN单元,那么你需要明确指定cell参数,例如: ``` python class CustomCell(tf.keras.layers.Layer): def __init__(self, units, **kwargs): super(CustomCell, self).__init__(**kwargs) self.units = units self.state_size = units def build(self, input_shape): self.kernel = self.add_weight( shape=(input_shape[-1], self.units), initializer='uniform', name='kernel') self.recurrent_kernel = self.add_weight( shape=(self.units, self.units), initializer='uniform', name='recurrent_kernel') self.bias = self.add_weight( shape=(self.units,), initializer='zeros', name='bias') self.built = True def call(self, inputs, states): prev_output = states[0] h = tf.matmul(inputs, self.kernel) output = h + tf.matmul(prev_output, self.recurrent_kernel) + self.bias return output, [output] model = tf.keras.Sequential([ tf.keras.layers.RNN(CustomCell(64), input_shape=(None, 10), return_sequences=True), tf.keras.layers.Dense(1) ]) ``` 在这个例子中,我们自定义了一个RNN单元CustomCell,并在实例化RNN层时指定了cell参数。 ### 回答2: 这个错误是因为在使用TensorFlow进行模型构建时,缺少了一个必需的位置参数'cell'。在TensorFlow中,'cell'是循环神经网络(RNN)中的一个重要组件,用于定义循环层的结构和行为。当构建循环神经网络时,我们需要在定义循环层时传入一个合适的循环单元(RNN cell)。 为了解决这个错误,我们需要确保在构建RNN模型时传入正确的循环单元参数。通常,我们可以使用TensorFlow中提供的RNN单元类,例如BasicRNNCell(基本RNN单元)、LSTMCell(长短期记忆单元)或GRUCell(门控循环单元)等来创建循环单元对象。然后,我们可以将这个循环单元作为参数传递给RNN层的构造函数。 下面是一个示例代码,演示了如何使用LSTM单元构建一个简单的循环神经网络模型: ```python import tensorflow as tf # 定义LSTM单元 lstm_cell = tf.keras.layers.LSTMCell(units=64) # 定义RNN层 rnn_layer = tf.keras.layers.RNN(cell=lstm_cell) # 通过RNN层构建模型 model = tf.keras.models.Sequential() model.add(rnn_layer) model.add(tf.keras.layers.Dense(units=10, activation='softmax')) # 打印模型结构 model.summary() ``` 在上述代码中,我们首先创建了一个LSTM单元(LSTMCell),然后将该LSTM单元作为参数传递给RNN层的构造函数。最后,我们通过Sequential模型将RNN层和一个全连接层(Dense)组合起来构建模型。 通过这种方式,我们可以解决"TypeError: __init__() missing 1 required positional argument: 'cell'"错误,并成功构建带有适当单元的循环神经网络模型。 ### 回答3: 这个错误是由于在使用TensorFlow时,没有正确初始化某个参数所导致的。具体来说,这个错误是因为在使用RNN模型时,没有正确传递一个名为"cell"的参数。 在TensorFlow中,循环神经网络(RNN)的实现需要使用一个叫做"cell"的对象,它定义了RNN中的基本单元。这个对象通常通过tf.keras.layers中的一些方法来创建,比如tf.keras.layers.SimpleRNNCell、tf.keras.layers.LSTMCell等。然后,这个cell对象可以通过tf.keras.layers.RNN或tf.keras.layers.SimpleRNN等高阶的RNN层来使用。 当出现上述错误时,说明在创建RNN对象时没有正确传递"cell"参数。为了解决这个问题,可以按照以下步骤进行操作: 1. 确保使用上述提到的合适的方法来创建cell对象,如tf.keras.layers.SimpleRNNCell或tf.keras.layers.LSTMCell。 2. 确保在使用RNN层时,将已创建的cell对象作为参数传递给RNN层的"cell"参数。 例如,正确的使用方式可能如下所示: ```python import tensorflow as tf # 创建cell对象 cell = tf.keras.layers.SimpleRNNCell(units=64) # 使用cell对象创建RNN层 rnn_layer = tf.keras.layers.RNN(cell) # 继续定义其他结构 ``` 按照上述方式,可以正确创建RNN模型,并且避免出现"TypeError: __init__() missing 1 required positional argument: 'cell'"错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值