mirror of
https://github.com/tensorflow/haskell.git
synced 2024-11-05 10:39:44 +01:00
98 lines
12 KiB
HTML
98 lines
12 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html>
|
|
<head>
|
|
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
|
|
<title>src/TensorFlow/NN.hs</title>
|
|
<link type='text/css' rel='stylesheet' href='hscolour.css' />
|
|
</head>
|
|
<body>
|
|
<pre><a name="line-1"></a><span class='hs-comment'>-- Copyright 2016 TensorFlow authors.</span>
|
|
<a name="line-2"></a><span class='hs-comment'>--</span>
|
|
<a name="line-3"></a><span class='hs-comment'>-- Licensed under the Apache License, Version 2.0 (the "License");</span>
|
|
<a name="line-4"></a><span class='hs-comment'>-- you may not use this file except in compliance with the License.</span>
|
|
<a name="line-5"></a><span class='hs-comment'>-- You may obtain a copy of the License at</span>
|
|
<a name="line-6"></a><span class='hs-comment'>--</span>
|
|
<a name="line-7"></a><span class='hs-comment'>-- <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a></span>
|
|
<a name="line-8"></a><span class='hs-comment'>--</span>
|
|
<a name="line-9"></a><span class='hs-comment'>-- Unless required by applicable law or agreed to in writing, software</span>
|
|
<a name="line-10"></a><span class='hs-comment'>-- distributed under the License is distributed on an "AS IS" BASIS,</span>
|
|
<a name="line-11"></a><span class='hs-comment'>-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
|
|
<a name="line-12"></a><span class='hs-comment'>-- See the License for the specific language governing permissions and</span>
|
|
<a name="line-13"></a><span class='hs-comment'>-- limitations under the License.</span>
|
|
<a name="line-14"></a>
|
|
<a name="line-15"></a><span class='hs-comment'>{-# LANGUAGE DataKinds #-}</span>
|
|
<a name="line-16"></a><span class='hs-comment'>{-# LANGUAGE OverloadedStrings #-}</span>
|
|
<a name="line-17"></a>
|
|
<a name="line-18"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>TensorFlow</span><span class='hs-varop'>.</span><span class='hs-conid'>NN</span>
|
|
<a name="line-19"></a> <span class='hs-layout'>(</span> <span class='hs-varid'>sigmoidCrossEntropyWithLogits</span>
|
|
<a name="line-20"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
|
|
<a name="line-21"></a>
|
|
<a name="line-22"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Prelude</span> <span class='hs-varid'>hiding</span> <span class='hs-layout'>(</span> <span class='hs-varid'>log</span>
|
|
<a name="line-23"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>exp</span>
|
|
<a name="line-24"></a> <span class='hs-layout'>)</span>
|
|
<a name="line-25"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>TensorFlow</span><span class='hs-varop'>.</span><span class='hs-conid'>Build</span> <span class='hs-layout'>(</span> <span class='hs-conid'>Build</span>
|
|
<a name="line-26"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>render</span>
|
|
<a name="line-27"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>withNameScope</span>
|
|
<a name="line-28"></a> <span class='hs-layout'>)</span>
|
|
<a name="line-29"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>TensorFlow</span><span class='hs-varop'>.</span><span class='hs-conid'>GenOps</span><span class='hs-varop'>.</span><span class='hs-conid'>Core</span> <span class='hs-layout'>(</span> <span class='hs-varid'>greaterEqual</span>
|
|
<a name="line-30"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>select</span>
|
|
<a name="line-31"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>log</span>
|
|
<a name="line-32"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>exp</span>
|
|
<a name="line-33"></a> <span class='hs-layout'>)</span>
|
|
<a name="line-34"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>TensorFlow</span><span class='hs-varop'>.</span><span class='hs-conid'>Tensor</span> <span class='hs-layout'>(</span> <span class='hs-conid'>Tensor</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
|
|
<a name="line-35"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>Value</span>
|
|
<a name="line-36"></a> <span class='hs-layout'>)</span>
|
|
<a name="line-37"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>TensorFlow</span><span class='hs-varop'>.</span><span class='hs-conid'>Types</span> <span class='hs-layout'>(</span> <span class='hs-conid'>TensorType</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
|
|
<a name="line-38"></a> <span class='hs-layout'>,</span> <span class='hs-conid'>OneOf</span>
|
|
<a name="line-39"></a> <span class='hs-layout'>)</span>
|
|
<a name="line-40"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>TensorFlow</span><span class='hs-varop'>.</span><span class='hs-conid'>Ops</span> <span class='hs-layout'>(</span> <span class='hs-varid'>zerosLike</span>
|
|
<a name="line-41"></a> <span class='hs-layout'>,</span> <span class='hs-varid'>add</span>
|
|
<a name="line-42"></a> <span class='hs-layout'>)</span>
|
|
<a name="line-43"></a>
|
|
<a name="line-44"></a><a name="sigmoidCrossEntropyWithLogits"></a><span class='hs-comment'>-- | Computes sigmoid cross entropy given `logits`.</span>
|
|
<a name="line-45"></a><span class='hs-comment'>--</span>
|
|
<a name="line-46"></a><span class='hs-comment'>-- Measures the probability error in discrete classification tasks in which each</span>
|
|
<a name="line-47"></a><span class='hs-comment'>-- class is independent and not mutually exclusive. For instance, one could</span>
|
|
<a name="line-48"></a><span class='hs-comment'>-- perform multilabel classification where a picture can contain both an elephant</span>
|
|
<a name="line-49"></a><span class='hs-comment'>-- and a dog at the same time.</span>
|
|
<a name="line-50"></a><span class='hs-comment'>--</span>
|
|
<a name="line-51"></a><span class='hs-comment'>-- For brevity, let `x = logits`, `z = targets`. The logistic loss is</span>
|
|
<a name="line-52"></a><span class='hs-comment'>--</span>
|
|
<a name="line-53"></a><span class='hs-comment'>-- z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))</span>
|
|
<a name="line-54"></a><span class='hs-comment'>-- = z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))</span>
|
|
<a name="line-55"></a><span class='hs-comment'>-- = z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))</span>
|
|
<a name="line-56"></a><span class='hs-comment'>-- = z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))</span>
|
|
<a name="line-57"></a><span class='hs-comment'>-- = (1 - z) * x + log(1 + exp(-x))</span>
|
|
<a name="line-58"></a><span class='hs-comment'>-- = x - x * z + log(1 + exp(-x))</span>
|
|
<a name="line-59"></a><span class='hs-comment'>--</span>
|
|
<a name="line-60"></a><span class='hs-comment'>-- For x < 0, to avoid overflow in exp(-x), we reformulate the above</span>
|
|
<a name="line-61"></a><span class='hs-comment'>--</span>
|
|
<a name="line-62"></a><span class='hs-comment'>-- x - x * z + log(1 + exp(-x))</span>
|
|
<a name="line-63"></a><span class='hs-comment'>-- = log(exp(x)) - x * z + log(1 + exp(-x))</span>
|
|
<a name="line-64"></a><span class='hs-comment'>-- = - x * z + log(1 + exp(x))</span>
|
|
<a name="line-65"></a><span class='hs-comment'>--</span>
|
|
<a name="line-66"></a><span class='hs-comment'>-- Hence, to ensure stability and avoid overflow, the implementation uses this</span>
|
|
<a name="line-67"></a><span class='hs-comment'>-- equivalent formulation</span>
|
|
<a name="line-68"></a><span class='hs-comment'>--</span>
|
|
<a name="line-69"></a><span class='hs-comment'>-- max(x, 0) - x * z + log(1 + exp(-abs(x)))</span>
|
|
<a name="line-70"></a><span class='hs-comment'>--</span>
|
|
<a name="line-71"></a><span class='hs-comment'>-- `logits` and `targets` must have the same type and shape.</span>
|
|
<a name="line-72"></a><span class='hs-definition'>sigmoidCrossEntropyWithLogits</span>
|
|
<a name="line-73"></a> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>OneOf</span> <span class='hs-chr'>'</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>Float</span><span class='hs-layout'>,</span> <span class='hs-conid'>Double</span><span class='hs-keyglyph'>]</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>TensorType</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>Num</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
|
|
<a name="line-74"></a> <span class='hs-keyglyph'>=></span> <span class='hs-conid'>Tensor</span> <span class='hs-conid'>Value</span> <span class='hs-varid'>a</span> <span class='hs-comment'>-- ^ __logits__</span>
|
|
<a name="line-75"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Tensor</span> <span class='hs-conid'>Value</span> <span class='hs-varid'>a</span> <span class='hs-comment'>-- ^ __targets__</span>
|
|
<a name="line-76"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Build</span> <span class='hs-layout'>(</span><span class='hs-conid'>Tensor</span> <span class='hs-conid'>Value</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
|
|
<a name="line-77"></a><span class='hs-definition'>sigmoidCrossEntropyWithLogits</span> <span class='hs-varid'>logits</span> <span class='hs-varid'>targets</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
|
|
<a name="line-78"></a> <span class='hs-varid'>logits'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>render</span> <span class='hs-varid'>logits</span>
|
|
<a name="line-79"></a> <span class='hs-varid'>targets'</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>render</span> <span class='hs-varid'>targets</span>
|
|
<a name="line-80"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>zeros</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>zerosLike</span> <span class='hs-varid'>logits'</span>
|
|
<a name="line-81"></a> <span class='hs-varid'>cond</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>logits'</span> <span class='hs-varop'>`greaterEqual`</span> <span class='hs-varid'>zeros</span>
|
|
<a name="line-82"></a> <span class='hs-varid'>relu_logits</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>select</span> <span class='hs-varid'>cond</span> <span class='hs-varid'>logits'</span> <span class='hs-varid'>zeros</span>
|
|
<a name="line-83"></a> <span class='hs-varid'>neg_abs_logits</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>select</span> <span class='hs-varid'>cond</span> <span class='hs-layout'>(</span><span class='hs-comment'>-</span><span class='hs-varid'>logits'</span><span class='hs-layout'>)</span> <span class='hs-varid'>logits'</span>
|
|
<a name="line-84"></a> <span class='hs-varid'>withNameScope</span> <span class='hs-str'>"logistic_loss"</span> <span class='hs-varop'>$</span> <span class='hs-keyword'>do</span>
|
|
<a name="line-85"></a> <span class='hs-varid'>left</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>render</span> <span class='hs-varop'>$</span> <span class='hs-varid'>relu_logits</span> <span class='hs-comment'>-</span> <span class='hs-varid'>logits'</span> <span class='hs-varop'>*</span> <span class='hs-varid'>targets'</span>
|
|
<a name="line-86"></a> <span class='hs-varid'>right</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>render</span> <span class='hs-varop'>$</span> <span class='hs-varid'>log</span> <span class='hs-layout'>(</span><span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-varid'>exp</span> <span class='hs-varid'>neg_abs_logits</span><span class='hs-layout'>)</span>
|
|
<a name="line-87"></a> <span class='hs-varid'>withNameScope</span> <span class='hs-str'>"sigmoid_add"</span> <span class='hs-varop'>$</span> <span class='hs-varid'>render</span> <span class='hs-varop'>$</span> <span class='hs-varid'>left</span> <span class='hs-varop'>`add`</span> <span class='hs-varid'>right</span>
|
|
</pre></body>
|
|
</html>
|