<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bias &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/bias/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 27 Apr 2020 17:41:00 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 如何取出 tf.layers.dense 定义的全连接层的weight和bias参数值</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 27 Sep 2019 09:24:54 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[bias]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[tf.layers.dense]]></category>
		<category><![CDATA[weight]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10578</guid>

					<description><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>在TensorFlow中，<span style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &#34;Droid Sans Mono&#34;; font-size: 13.5pt;">tf.layers.dense</span>&#160;定义了一个全连接层，其实现的是（来自官方文档）：</p>
<blockquote>
<p>
		This layer implements the operation: outputs = activation(inputs * kernel + bias) Where activation is the activation function passed as the activation argument (if not None), kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer (only if use_bias is True).</p></blockquote>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>在TensorFlow中，<span style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;Droid Sans Mono&quot;; font-size: 13.5pt;">tf.layers.dense</span>&nbsp;定义了一个全连接层，其实现的是（来自官方文档）：</p>
<blockquote>
<p>
		This layer implements the operation: outputs = activation(inputs * kernel + bias) Where activation is the activation function passed as the activation argument (if not None), kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer (only if use_bias is True).</p>
</blockquote>
<p>意思就是它实现了 y = activation(x * kernel + bias) 的操作，其中，activation是激活函数。在这里，<span style="color:#0000ff;">kernel</span> 就是指我们通常所说的 weight，它被TF称为&nbsp;<span style="color:#0000ff;">kernel</span>&nbsp;而不是 weight。因此，如果你想从这个模型里取出weight参数的话，就要注意它的名字了，否则会读不到这个参数。<br />
下面我们就来看具体的例子。<br />
<span id="more-10578"></span><br />
下面这段代码的逻辑非常简单，它干了下面这些事情：<br />
<span style="color:#800000;">* 创建了一个非常简单的2层全连接网络<br />
* 训练网络<br />
* 训练完成后把它保存成checkpoint格式的模型文件<br />
* 从保存的checkpoint读取模型里第一层的 weight 和 bias 参数值，打印出来</span><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a></p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
<span style="color:#cc7832;">import </span>tensorflow <span style="color:#cc7832;">as </span>tf
<span style="color:#cc7832;">import </span>numpy <span style="color:#cc7832;">as </span>np

LAYER_1_NAME = <span style="color:#6a8759;">&#39;layer1&#39;  </span><span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">第一层的名字
</span>LAYER_2_NAME = <span style="color:#6a8759;">&#39;layer2&#39;  </span><span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">第二层的名字
</span>
<span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">创建一个非常简单的神经网络，它有两层
</span>x = tf.placeholder(<span style="color:#aa4926;">shape</span>=[<span style="color:#cc7832;">None, </span><span style="color:#6897bb;">2</span>]<span style="color:#cc7832;">, </span><span style="color:#aa4926;">dtype</span>=tf.float32)
layer1 = tf.layers.dense(x<span style="color:#cc7832;">, </span><span style="color:#6897bb;">5</span><span style="color:#cc7832;">, </span><span style="color:#aa4926;">activation</span>=tf.nn.sigmoid<span style="color:#cc7832;">, </span><span style="color:#aa4926;">name</span>=LAYER_1_NAME)
layer2 = tf.layers.dense(layer1<span style="color:#cc7832;">, </span><span style="color:#6897bb;">2</span><span style="color:#cc7832;">, </span><span style="color:#aa4926;">activation</span>=tf.nn.sigmoid<span style="color:#cc7832;">, </span><span style="color:#aa4926;">name</span>=LAYER_2_NAME)

loss = tf.reduce_mean((layer2 - x) ** <span style="color:#6897bb;">2</span>)
optimizer = tf.train.AdamOptimizer(<span style="color:#6897bb;">0.01</span>).minimize(loss)

<span style="color:#cc7832;">with </span>tf.Session() <span style="color:#cc7832;">as </span>sess:
    sess.run(tf.global_variables_initializer())
    x_values = np.random.normal(<span style="color:#6897bb;">0</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">1</span><span style="color:#cc7832;">, </span>(<span style="color:#6897bb;">5000</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">2</span>))  <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">生成用于输入的随机数
</span><span style="color:#808080;font-family:'AR PL UKai CN';">    </span><span style="color:#cc7832;">for </span>step <span style="color:#cc7832;">in </span><span style="color:#8888c6;">range</span>(<span style="color:#6897bb;">1000</span>):
        _<span style="color:#cc7832;">, </span>loss_value = sess.run([optimizer<span style="color:#cc7832;">, </span>loss]<span style="color:#cc7832;">, </span><span style="color:#aa4926;">feed_dict</span>={x: x_values})
        <span style="color:#cc7832;">if </span>step % <span style="color:#6897bb;">100 </span>== <span style="color:#6897bb;">0</span>:
            <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&quot;step: %d, loss: %f&quot; </span>% (step<span style="color:#cc7832;">, </span>loss_value))

    <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">把模型保存成</span><span style="color:#808080;">checkpoint
</span><span style="color:#808080;">    </span>saver = tf.compat.v1.train.Saver()
    save_path = saver.save(sess<span style="color:#cc7832;">, </span><span style="color:#6a8759;">&#39;./checkpoint/model.ckpt&#39;</span>)
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&quot;model saved in path: %s&quot; </span>% save_path<span style="color:#cc7832;">, </span><span style="color:#aa4926;">flush</span>=<span style="color:#cc7832;">True</span>)

    <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">读取刚保存的</span><span style="color:#808080;">checkpoint
</span><span style="color:#808080;">    </span>reader = tf.train.NewCheckpointReader(save_path)
    weights = reader.get_tensor(LAYER_1_NAME + <span style="color:#6a8759;">&#39;/kernel&#39;</span>)  <span style="color:#808080;"># weight</span><span style="color:#808080;font-family:'AR PL UKai CN';">的名字，是由对应层的名字，加上默认的&quot;kernel&quot;组成的</span><span style="color:#808080;">
</span><span style="color:#808080;">    </span>bias = reader.get_tensor(LAYER_1_NAME + <span style="color:#6a8759;">&#39;/bias&#39;</span>)  <span style="color:#808080;"># bias</span><span style="color:#808080;font-family:'AR PL UKai CN';">的名字
</span><span style="color:#808080;font-family:'AR PL UKai CN';">    </span><span style="color:#8888c6;">print</span>(weights)
    <span style="color:#8888c6;">print</span>(bias)

    <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">如果想打印模型中的所有参数名和参数值的话，把下面几行取消注释
</span><span style="color:#808080;font-family:'AR PL UKai CN';">    </span><span style="color:#808080;"># var_to_shape_map = reader.get_variable_to_shape_map()
</span><span style="color:#808080;">    # for key in var_to_shape_map:
</span><span style="color:#808080;">    #     print(&quot;tensor name: &quot;, key)
</span><span style="color:#808080;">    #     print(reader.get_tensor(key))</span></pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
上面程序的输出：</p>
<blockquote>
<div>
		step: 0, loss: 1.166642</div>
<div>
		step: 100, loss: 0.764896</div>
<div>
		step: 200, loss: 0.659553</div>
<div>
		step: 300, loss: 0.622590</div>
<div>
		step: 400, loss: 0.604577</div>
<div>
		step: 500, loss: 0.595308</div>
<div>
		step: 600, loss: 0.590193</div>
<div>
		step: 700, loss: 0.587147</div>
<div>
		step: 800, loss: 0.585213</div>
<div>
		step: 900, loss: 0.583916</div>
<div>
		model saved in path: ./checkpoint/model.ckpt</div>
<div>
		[[-3.0937738&nbsp; &nbsp;3.6378026&nbsp; &nbsp;0.91563785 -1.2663897&nbsp; -0.09645918]</div>
<div>
		&nbsp;[ 0.727915&nbsp; &nbsp; 0.22842272 -2.656725&nbsp; &nbsp;-2.0831287&nbsp; -3.4844823 ]]</div>
<div>
		[ 0.906906&nbsp; &nbsp;-1.6009021&nbsp; &nbsp;0.46897942&nbsp; 0.75946033&nbsp; 1.4348172 ]</div>
</blockquote>
<p>
注意，在取某一层的 weight 参数值的时候，我们用了以下方法：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
weights = reader.get_tensor(LAYER_1_NAME + <span style="color:#6a8759;">&#39;/kernel&#39;</span>)</pre>
<p>这里，weight 的名字，是类似于 &quot;<span style="color:#ff0000;">layer1/kernel</span>&quot; 这样的字符串，其中，&quot;<span style="color:#b22222;">layer1</span>&quot;就是在用&nbsp;tf.layers.dense() 创建网络的一个layer的时候，设置的 &quot;name=xxx&quot; 里的名字。前面已经说过了，TF 里把 weight 叫做 <span style="color:#0000ff;">kernel</span>，所以这里的 weight 名是 &quot;<span style="color: rgb(255, 0, 0);">layer1/kernel</span>&quot; 而不是 &quot;<span style="color: rgb(255, 0, 0);">layer1/weight</span>&quot;！<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
