提交 a9ff50b8 authored 作者: Christof Angermueller's avatar Christof Angermueller

Add more complex examples

上级 8540e47d
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Table of Contents\n",
"* [Model](#Model)\n",
"* [Example 1](#Example-1)\n",
"* [Example 2](#Example-2)\n",
"* [Example 3](#Example-3)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy\n",
"import theano\n",
"import theano.tensor as T\n",
"import theano.printing as pr\n",
"import theano.d3printing as d3p\n",
"rng = numpy.random"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Model "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Training data\n",
"N = 400\n",
"feats = 784\n",
"D = (rng.randn(N, feats).astype(theano.config.floatX), rng.randint(size=N,low=0, high=2).astype(theano.config.floatX))\n",
"training_steps = 10000\n",
"\n",
"# Declare Theano symbolic variables\n",
"x = T.matrix(\"x\")\n",
"y = T.vector(\"y\")\n",
"w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name=\"w\")\n",
"b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name=\"b\")\n",
"x.tag.test_value = D[0]\n",
"y.tag.test_value = D[1]\n",
"\n",
"# Construct Theano expression graph\n",
"p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probability of having a one\n",
"prediction = p_1 > 0.5 # The prediction that is done: 0 or 1\n",
"\n",
"# Compute gradients\n",
"xent = -y*T.log(p_1) - (1-y)*T.log(1-p_1) # Cross-entropy\n",
"cost = xent.mean() + 0.01*(w**2).sum() # The cost to optimize\n",
"gw,gb = T.grad(cost, [w,b])\n",
"\n",
"# Training and prediction function\n",
"train = theano.function(inputs=[x,y], outputs=[prediction, xent], updates=[[w, w-0.01*gw], [b, b-0.01*gb]], name = \"train\")\n",
"predict = theano.function(inputs=[x], outputs=prediction, name = \"predict\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example 1 "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output file is available at p1.png\n"
]
}
],
"source": [
"pr.pydotprint(p_1, outfile='p1.png', var_with_name_simple=True)\n",
"d3p.d3write(p_1, 'p1.dot')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href='p1.html'><img src='p1.png'/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[open](./p1.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example 2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output file is available at predict.png\n"
]
}
],
"source": [
"pr.pydotprint(predict, outfile='predict.png', var_with_name_simple=True)\n",
"d3p.d3write(predict, 'predict.dot')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href='predict.html'><img src='predict.png'/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[open](./predict.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example 3"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output file is available at train.png\n"
]
}
],
"source": [
"pr.pydotprint(train, outfile='train.png', var_with_name_simple=True)\n",
"d3p.d3write(train, 'train.dot')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href='train.html'><img src='train.png'/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[open](./train.html)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
digraph G { graph [bb="0,0,719,672"]; "DimShuffle{x}" [height=0.5, pos="558,478", shape=ellipse, width=1.8374]; "Elemwise{sub,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="461,390", shape=ellipse, style=filled, width=3.0624]; "DimShuffle{x}" -> "Elemwise{sub,no_inplace}" [label="1 TensorType(float64, (True,))", lp="632.5,434", pos="e,506.93,406.57 553.52,459.62 549.99,448.82 544.12,435.29 535,426 529.47,420.37 522.85,415.53 515.89,411.41"]; "name=b TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="558,566", shape=box, style=filled, width=3.0625]; "name=b TensorType(float64, scalar)" -> "DimShuffle{x}" [label="TensorType(float64, scalar)", lp="636,522", pos="e,558,496.08 558,547.6 558,535.75 558,519.82 558,506.29"]; dot [height=0.5, pos="363,566", shape=ellipse, width=0.75]; "Elemwise{neg,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="363,478", shape=ellipse, style=filled, width=3.0624]; dot -> "Elemwise{neg,no_inplace}" [label="TensorType(float64, vector)", lp="442,522", pos="e,363,496.08 363,547.6 363,535.75 363,519.82 363,506.29"]; "name=x TensorType(float64, matrix)" [fillcolor=limegreen, height=0.5, pos="241,654", shape=box, style=filled, width=3.1181]; "name=x TensorType(float64, matrix)" -> dot [label="0 TensorType(float64, matrix)", lp="378,610", pos="e,340.56,576.07 255.77,636 265.61,625.35 279.21,611.84 293,602 304.81,593.57 318.95,586.09 331.42,580.22"]; "name=w TensorType(float64, vector)" [fillcolor=limegreen, height=0.5, pos="485,654", shape=box, style=filled, width=3.1389]; "name=w TensorType(float64, vector)" -> dot [label="1 TensorType(float64, vector)", lp="559.5,610", pos="e,389.41,570.28 481.35,635.78 478.2,624.78 472.59,610.94 463,602 445.54,585.73 420.08,576.96 399.23,572.27"]; "DimShuffle{x} id=2" [height=0.5, pos="247,302", shape=ellipse, width=2.3721]; "Elemwise{add,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="369,194", shape=ellipse, style=filled, width=3.0624]; "DimShuffle{x} id=2" -> "Elemwise{add,no_inplace}" [label="0 TensorType(int8, (True,))", lp="334,248", pos="e,290.21,206.6 244.34,283.76 242.92,267.96 243.54,244.85 256,230 262.8,221.89 271.45,215.63 280.91,210.8"]; "val=1 TensorType(int8, scalar)" [fillcolor=limegreen, height=0.5, pos="161,390", shape=box, style=filled, width=2.6389]; "val=1 TensorType(int8, scalar)" -> "DimShuffle{x} id=2" [label="TensorType(int8, scalar)", lp="305.5,346", pos="e,242.7,320.2 201.81,371.98 210.24,367.09 218.52,361.12 225,354 231.25,347.13 235.92,338.22 239.3,329.76"]; "DimShuffle{x} id=3" [height=0.5, pos="85,248", shape=ellipse, width=2.3721]; "val=1 TensorType(int8, scalar)" -> "DimShuffle{x} id=3" [label="TensorType(int8, scalar)", lp="151.5,346", pos="e,78.947,266.23 101.23,371.9 93.625,367.28 86.827,361.42 82,354 66.992,330.94 70.718,298.58 76.308,275.9"]; "Elemwise{true_div,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="164,106", shape=ellipse, style=filled, width=3.5561]; "DimShuffle{x} id=3" -> "Elemwise{true_div,no_inplace}" [label="0 TensorType(int8, (True,))", lp="172,194", pos="e,140.09,123.74 84.103,229.75 84.024,214.78 85.627,192.93 94,176 102.79,158.23 118.02,142.37 132.04,130.36"]; "Elemwise{neg,no_inplace}" -> "Elemwise{sub,no_inplace}" [label="0 TensorType(float64, vector)", lp="446.5,434", pos="e,389.83,403.99 357.87,459.89 355.74,449.21 355.15,435.69 362,426 366.99,418.94 373.53,413.29 380.88,408.78"]; "Elemwise{exp,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="461,302", shape=ellipse, style=filled, width=3.0624]; "Elemwise{sub,no_inplace}" -> "Elemwise{exp,no_inplace}" [label="TensorType(float64, vector)", lp="540,346", pos="e,461,320.08 461,371.6 461,359.75 461,343.82 461,330.29"]; "Elemwise{exp,no_inplace}" -> "Elemwise{add,no_inplace}" [label="1 TensorType(float64, vector)", lp="526.5,248", pos="e,394.8,211.6 452.54,284.02 444.46,268.82 431.28,246.48 416,230 412.01,225.7 407.43,221.56 402.74,217.73"]; "Elemwise{add,no_inplace}" -> "Elemwise{true_div,no_inplace}" [label="1 TensorType(float64, vector)", lp="369.5,150", pos="e,202.57,123.18 330.92,177.03 297.33,162.93 248.22,142.33 211.96,127.12"]; "TensorType(float64, vector) id=12" [fillcolor=dodgerblue, height=0.5, pos="164,18", shape=box, style=filled, width=2.9236]; "Elemwise{true_div,no_inplace}" -> "TensorType(float64, vector) id=12" [label="TensorType(float64, vector)", lp="243,62", pos="e,164,36.084 164,87.597 164,75.746 164,59.817 164,46.292"]; }
\ No newline at end of file
......@@ -14,7 +14,7 @@
display:block;
position: fixed;
border: 0px solid black;
top:0%; left:0%; right:0% bottom=0%
top:5%; left:0%; right:0% bottom=10%
}
.nodeRect {
stroke: black;
......@@ -28,7 +28,6 @@
color: black;
}
.edge {
stroke: black;
stroke-width: 3px;
cursor: pointer;
}
......@@ -43,17 +42,33 @@
text-anchor: start;
}
.arrowHead {
stroke: black;
stroke: green;
stroke-width: 1px;
}
.arrowHead_n {
stroke: green;
}
.arrowHead_r {
stroke-width: 3px;
fill: black;
fill: red;
stroke: red;
}
.arrowHead_b {
stroke: dodgerblue;
}
</style>
<div>
<input name="resetNodes"
type="button"
value="Reset nodes"
onclick="resetNodes()"/>
<input name="releaseNodes"
type="button"
value="Release nodes"
onclick="releaseNodes()"/>
</div>
<script type="text/javascript">
var path='logreg.dot';
var width = 1200;
var height = 900;
var path='p1.dot';
// Global attributes
var pad = 10;
......@@ -61,12 +76,16 @@
var svg = d3.select('body').append('svg')
.attr('class', 'svg')
.attr('width', '100%')
.attr('height', '100%');
.attr('height', '95%');
var pane = svg.append('g');
// Definition head of edges
svg.append("defs").append("marker")
.attr("id", 'edgeArrow')
var markerData = [
{'id': 'n', 'color': 'black'},
{'id': 'r', 'color': 'red'},
{'id': 'b', 'color': 'dodgerblue'}];
svg.append("defs").selectAll('marker').data(markerData).enter().append("marker")
.attr("id", function(d) { return 'edgeArrow_' + d.id;})
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("refX", 3)
......@@ -74,7 +93,7 @@
.attr("orient", "auto")
.append("path")
.attr("d", "M0,0 L6,3 L0,6 Z")
.attr('style', 'arrowHead');
.attr('fill', function(d) { return d.color;});
function textSize(text, attr) {
var t = svg.append('text').text(text);
......@@ -87,11 +106,21 @@
t.remove();
return bbox;
}
function exists(x) {
return typeof(x) != 'undefined';
}
var dotGraph;
var graph = {};
var nodes = [];
var edges = [];
var layout;
var scaleDotX;
var scaleDotY;
d3.text(path, function(data) {
dotGraph = graphlibDot.parse(data);
......@@ -107,10 +136,10 @@
}
dotWidth = posMax[0];
dotHeight = posMax[1];
svg.attr('viewBox', '0,0,' + width + ',' + height);
svg.attr('viewBox', '0,0,' + dotWidth + ',' + dotHeight);
var scaleDotX = d3.scale.linear().domain([0, dotWidth + 100]).range([0, width]);
var scaleDotY = d3.scale.linear().domain([-25, dotHeight + 100]).range([0, height]);
scaleDotX = d3.scale.linear().domain([0, dotWidth]).range([0, dotWidth]);
scaleDotY = d3.scale.linear().domain([0, dotHeight]).range([0, dotHeight]);
// Parse nodes
var i = 0;
......@@ -139,6 +168,19 @@
var size = textSize(edge.value.label, {'class': 'edgeLabelText'});
edge.value.width = size.width + 2 * pad;
edge.value.height = size.height + 2 * pad;
if (!exists(edge.value.color)) {
edge.value.color = 'black';
}
switch (edge.value.color) {
case 'dodgerblue':
edge.value.type = 'b';
break;
case 'red':
edge.value.type = 'r';
break;
default:
edge.value.type = 'n';
}
edges.push(edge);
dotGraph._edges[edgeId] = edge;
}
......@@ -153,7 +195,8 @@
// Add edges
edges = pane.append('g').attr('id', 'edges').selectAll('path').data(graph['edges']).enter().append('path')
.attr('class', 'edge')
.attr('marker-mid', 'url(#edgeArrow)')
.attr('stroke', function(d) {return d.value.color;})
.attr('marker-mid', function(d) { return 'url(#edgeArrow_' + d.value.type + ')';})
.on('mouseover', function(d) {
var edgeLabel = pane.select('#edgeLabels .' + d.id);
edgeLabel.attr('style', 'display: inline');
......@@ -218,25 +261,16 @@
}
shape.attr('fill', fillColor(d.value.fillcolor));
});
nodes.on('dblclick', releaseNode);
var nodesText = nodes.append('text')
.attr('class', 'nodeText')
.attr('x', pad)
.attr('dy', function(d) {return d.value.height - pad - 5;})
.text(function(d) {return d.value.label;});
.text(function(d) {return d.value.label;})
.on('dblclick', releaseNode);
// Update graph
function updateGraph() {
// Update nodes
nodes.attr('transform', function(d) { return 'translate(' + (d.x - d.value.cx) + ' ' + (d.y - d.value.cy) + ')'; });
// Update edges
edges.attr('d', function(d) {
return 'M' + d.source.x + ',' + d.source.y + ' L' + 0.5 * (d.source.x + d.target.x) + ',' + 0.5 * (d.source.y + d.target.y) + ' L' + d.target.x + ',' + d.target.y;
});
// Update edge labels
edgeLabels.attr('transform', function(d) {
return 'translate(' + (0.5 * (d.source.x + d.target.x - d.value.width)) + ',' + (0.5 * (d.source.y + d.target.y - d.value.height)) + ')';
});
}
// Drag-start event handler
function dragStart(d) {
......@@ -244,18 +278,18 @@
d.fixed = true;
}
// Zoom and translate event handler
function zoom(d) {
pane.attr('transform', 'translate(' + d3.event.translate + ') scale(' + d3.event.scale + ')');
}
// Force layout
var layout = d3.layout.force()
layout = d3.layout.force()
.nodes(graph['nodes'])
.links(graph['edges'])
.size([width, height])
.gravity(0.00)
.charge(-1000)
.size([dotWidth, dotHeight])
.charge(-3000)
.linkDistance(50)
.linkStrength(0.1)
.on('tick', updateGraph);
......@@ -268,12 +302,54 @@
// Zoom behaviour
var bZoom = d3.behavior.zoom()
.scaleExtent([0.2, 8])
//.on("dblclick.zoom", null)
.on('zoom', zoom);
svg.call(bZoom);
svg.on("dblclick.zoom", null);
// Start force layout
layout.start();
});
// Update graph
function updateGraph() {
// Update nodes
nodes.attr('transform', function(d) { return 'translate(' + (d.x - d.value.cx) + ' ' + (d.y - d.value.cy) + ')'; });
// Update edges
edges.attr('d', function(d) {
return 'M' + d.source.x + ',' + d.source.y + ' L' + 0.5 * (d.source.x + d.target.x) + ',' + 0.5 * (d.source.y + d.target.y) + ' L' + d.target.x + ',' + d.target.y;
});
// Update edge labels
edgeLabels.attr('transform', function(d) {
return 'translate(' + (0.5 * (d.source.x + d.target.x - d.value.width)) + ',' + (0.5 * (d.source.y + d.target.y - d.value.height)) + ')';
});
}
function releaseNode(d) {
d.fixed = false;
layout.start();
}
function releaseNodes() {
graph['nodes'].forEach (function (d) {
d.fixed = false;
});
layout.start();
}
function resetNodes() {
layout.stop();
var nodes = graph['nodes'];
nodes.forEach(function (node, i){
nodes[i].x = scaleDotX(node.value.pos[0]);
nodes[i].y = scaleDotY(dotHeight - (node.value.pos[1] + node.value.height));
nodes[i].px = nodes[i].x;
nodes[i].py = nodes[i].y;
nodes[i].fixed = true;
});
updateGraph();
layout.start();
}
</script>
</body>
</html>
digraph G { graph [bb="0,0,1297,476"]; "InplaceDimShuffle{x}" [height=0.5, pos="1120,194", shape=ellipse, width=2.5686]; "Elemwise{Composite{GT(scalar_sigmoid((-((-i0) - i1))), i2)}}" [fillcolor="#FFAABB", height=0.5, pos="822,106", shape=ellipse, style=filled, width=6.6733]; "InplaceDimShuffle{x}" -> "Elemwise{Composite{GT(scalar_sigmoid((-((-i0) - i1))), i2)}}" [label="1 TensorType(float64, (True,))", lp="1130.5,150", pos="e,919.34,122.55 1087.3,177.07 1063.3,165.92 1029.7,151.41 999,142 976.72,135.18 952.45,129.37 929.22,124.56"]; "name=b TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="1155,282", shape=box, style=filled, width=3.0625]; "name=b TensorType(float64, scalar)" -> "InplaceDimShuffle{x}" [color=dodgerblue, label="TensorType(float64, scalar)", lp="1219,238", pos="e,1127,212.08 1147.9,263.6 1143,251.51 1136.4,235.18 1130.8,221.49"]; "Shape_i{0}" [fillcolor=cyan, height=0.5, pos="123,370", shape=ellipse, style=filled, width=1.4763]; "AllocEmpty{dtype='float64'}" [fillcolor="#FFAA22", height=0.5, pos="117,282", shape=ellipse, style=filled, width=3.2589]; "Shape_i{0}" -> "AllocEmpty{dtype='float64'}" [label="TensorType(int64, scalar)", lp="194,326", pos="e,118.19,300.08 121.79,351.6 120.96,339.75 119.85,323.82 118.9,310.29"]; "name=x TensorType(float64, matrix)" [fillcolor=limegreen, height=0.5, pos="212,458", shape=box, style=filled, width=3.1181]; "name=x TensorType(float64, matrix)" -> "Shape_i{0}" [label="TensorType(float64, matrix)", lp="206,414", pos="e,119.68,388.12 146.59,439.97 138.58,435.35 131.35,429.47 126,422 121.13,415.21 119.44,406.5 119.25,398.22"]; "CGemv{inplace}" [height=0.5, pos="472,194", shape=ellipse, width=2.0569]; "name=x TensorType(float64, matrix)" -> "CGemv{inplace}" [label="2 TensorType(float64, matrix)", lp="387,326", pos="e,417.53,206.33 264.19,439.95 272.46,435.26 280.19,429.37 286,422 330.4,365.66 274.09,319.94 319,264 341.06,236.53 376.41,219.61 \ 407.58,209.41"]; "AllocEmpty{dtype='float64'}" -> "CGemv{inplace}" [color=red, label="0 TensorType(float64, vector)", lp="201.5,238", pos="e,398.78,197.07 111.67,263.77 109.42,252.76 108.91,238.92 117,230 134.92,210.23 289.58,201.22 388.77,197.44"]; "CGemv{inplace}" -> "Elemwise{Composite{GT(scalar_sigmoid((-((-i0) - i1))), i2)}}" [label="0 TensorType(float64, vector)", lp="733.5,150", pos="e,736.34,122.9 515.74,179.33 551.4,168.44 603.18,153.22 649,142 673.96,135.89 701.14,130.03 726.39,124.9"]; "val=1.0 TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="437,282", shape=box, style=filled, width=3.0278]; "val=1.0 TensorType(float64, scalar)" -> "CGemv{inplace}" [label="1 TensorType(float64, scalar)", lp="541.5,238", pos="e,465.05,212.08 444.08,263.6 449,251.51 455.65,235.18 461.22,221.49"]; "name=w TensorType(float64, vector)" [fillcolor=limegreen, height=0.5, pos="677,282", shape=box, style=filled, width=3.1389]; "name=w TensorType(float64, vector)" -> "CGemv{inplace}" [label="3 TensorType(float64, vector)", lp="733.5,238", pos="e,540.12,201.21 665.1,263.77 656.39,252.47 643.58,238.3 629,230 605.11,216.4 576.39,208.09 550.07,203"]; "val=0.0 TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="917,282", shape=box, style=filled, width=3.0278]; "val=0.0 TensorType(float64, scalar)" -> "CGemv{inplace}" [label="4 TensorType(float64, scalar)", lp="944.5,238", pos="e,541.25,200.46 891.6,263.79 873.13,252.18 847.07,237.65 822,230 796.82,222.32 646.71,209.1 551.43,201.29"]; "TensorType(int8, vector)" [fillcolor=dodgerblue, height=0.5, pos="822,18", shape=box, style=filled, width=2.1736]; "Elemwise{Composite{GT(scalar_sigmoid((-((-i0) - i1))), i2)}}" -> "TensorType(int8, vector)" [label="TensorType(int8, vector)", lp="892.5,62", pos="e,822,36.084 822,87.597 822,75.746 822,59.817 822,46.292"]; "val=[ 0.5] TensorType(float32, (True,))" [fillcolor=limegreen, height=0.5, pos="822,194", shape=box, style=filled, width=3.2847]; "val=[ 0.5] TensorType(float32, (True,))" -> "Elemwise{Composite{GT(scalar_sigmoid((-((-i0) - i1))), i2)}}" [label="2 TensorType(float32, (True,))", lp="908.5,150", pos="e,822,124.08 822,175.6 822,163.75 822,147.82 822,134.29"]; }
\ No newline at end of file
差异被折叠。
差异被折叠。
digraph G { graph [bb="0,0,1253,300"]; <theano.compile.builders.OpFromGraph object at 0x108543150> [height=0.5, pos="796,194", shape=ellipse, width=6.6504]; "Elemwise{Add}[(0, 0)]" [fillcolor="#FFAABB", height=0.5, pos="574,106", shape=ellipse, style=filled, width=2.6784]; <theano.compile.builders.OpFromGraph object at 0x108543150> -> "Elemwise{Add}[(0, 0)]" [label="1 TensorType(float64, scalar)", lp="788.5,150", pos="e,613.86,122.44 752.67,176.21 715.56,161.84 662.14,141.14 623.38,126.13"]; "name=z TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="283,282", shape=box, style=filled, width=3.0556]; "name=z TensorType(float64, scalar)" -> <theano.compile.builders.OpFromGraph object at 0x108543150> [label="0 TensorType(float64, scalar)", lp="615.5,238", pos="e,616.6,205.99 393.04,275.26 431.5,270.38 474.23,261.62 511,246 521.8,241.41 521.15,234.48 532,230 555.5,220.3 581.07,213.16 606.76,\ 207.91"]; "<theano.compile.builders.OpFromGraph object at 0x108543150> id=1" [height=0.5, pos="269,194", shape=ellipse, width=7.4733]; "name=z TensorType(float64, scalar)" -> "<theano.compile.builders.OpFromGraph object at 0x108543150> id=1" [label="2 TensorType(float64, scalar)", lp="237.5,238", pos="e,173.82,210.95 189.63,263.91 173.08,259.01 159.39,253.05 154,246 143.63,232.44 150.31,222.41 164.6,215.01"]; "name=y TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="972,282", shape=box, style=filled, width=3.0625]; "name=y TensorType(float64, scalar)" -> <theano.compile.builders.OpFromGraph object at 0x108543150> [label="1 TensorType(float64, scalar)", lp="1169.5,238", pos="e,965.46,206.74 1061.5,263.98 1082,256.18 1094.4,245.18 1081,230 1072.8,220.68 1027.6,213.31 975.6,207.78"]; "name=y TensorType(float64, scalar)" -> "<theano.compile.builders.OpFromGraph object at 0x108543150> id=1" [label="1 TensorType(float64, scalar)", lp="993.5,238", pos="e,458.53,206.81 939.13,263.94 929.51,258.55 919.14,252.34 910,246 900.93,239.7 901.27,234.05 891,230 819.8,201.88 623.43,216.34 \ 547,212 521.64,210.56 494.97,209 468.62,207.42"]; "name=x TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="663,282", shape=box, style=filled, width=3.0625]; "name=x TensorType(float64, scalar)" -> <theano.compile.builders.OpFromGraph object at 0x108543150> [label="2 TensorType(float64, scalar)", lp="807.5,238", pos="e,756.94,211.85 681.04,263.9 692.72,253.35 708.59,239.98 724,230 731.46,225.17 739.69,220.57 747.83,216.39"]; "name=x TensorType(float64, scalar)" -> "<theano.compile.builders.OpFromGraph object at 0x108543150> id=1" [label="0 TensorType(float64, scalar)", lp="427.5,238", pos="e,294.44,212.01 552.41,274.68 475.8,269.2 381.17,259.93 344,246 332.34,241.63 331.28,237.03 321,230 315.21,226.04 309.03,221.85 \ 303.04,217.8"]; "<theano.compile.builders.OpFromGraph object at 0x108543150> id=1" -> "Elemwise{Add}[(0, 0)]" [color=red, label="0 TensorType(float64, scalar)", lp="532.5,150", pos="e,522.97,121.39 327.81,176.42 381.1,161.39 459.3,139.34 513.2,124.14"]; "TensorType(float64, scalar) id=5" [fillcolor=dodgerblue, height=0.5, pos="574,18", shape=box, style=filled, width=2.7847]; "Elemwise{Add}[(0, 0)]" -> "TensorType(float64, scalar) id=5" [label="TensorType(float64, scalar)", lp="652,62", pos="e,574,36.084 574,87.597 574,75.746 574,59.817 574,46.292"]; }
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Couldn't import dot_parser, loading of dot files will not be possible.\n"
]
}
],
"source": [
"import numpy\n",
"import theano as th\n",
"import theano.tensor as T\n",
"import theano.printing as pr\n",
"import theano.d3printing as d3p"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x, y, z = T.scalars('xyz')\n",
"a = x + y + z\n",
"p = T.nnet.sigmoid(a**2)\n",
"op = th.OpFromGraph([x, y, z], [p])\n",
"\n",
"comp = op(x, y, z) + op(z, y, x)\n",
"comp_fun = th.function([x, y, z], [comp])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OpFromGraph "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[array(2.0)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comp_fun(1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output file is available at comp_fun.png\n"
]
}
],
"source": [
"pr.pydotprint(comp_fun, outfile='comp_fun.png', var_with_name_simple=True)\n",
"d3p.d3write(comp_fun, 'comp_fun.dot')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='comp_fun.png'/>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Idea "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='comp_fun1.png'/>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Clicking on one of OpFromGraph nodes will open the following sub-graph:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output file is available at p.png\n"
]
}
],
"source": [
"pr.pydotprint(p, outfile='p.png', var_with_name_simple=True)\n",
"d3p.d3write(p, 'p.dot')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src='p.png'/>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
digraph G { graph [bb="0,0,707,476"]; "Elemwise{add,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="469,370", shape=ellipse, style=filled, width=3.0624]; "Elemwise{add,no_inplace} id=1" [fillcolor="#FFAABB", height=0.5, pos="338,282", shape=ellipse, style=filled, width=3.5971]; "Elemwise{add,no_inplace}" -> "Elemwise{add,no_inplace} id=1" [label="0 TensorType(float64, scalar)", lp="524.5,326", pos="e,386.71,298.71 456.89,352.04 448.41,341.15 436.26,327.34 423,318 414.78,312.21 405.46,307.15 396.08,302.81"]; "name=x TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="349,458", shape=box, style=filled, width=3.0625]; "name=x TensorType(float64, scalar)" -> "Elemwise{add,no_inplace}" [label="0 TensorType(float64, scalar)", lp="432.5,414", pos="e,383.02,381.29 344.15,439.76 342.21,429.02 341.88,415.49 349,406 355.58,397.23 364.24,390.55 373.9,385.49"]; "name=y TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="588,458", shape=box, style=filled, width=3.0625]; "name=y TensorType(float64, scalar)" -> "Elemwise{add,no_inplace}" [label="1 TensorType(float64, scalar)", lp="623.5,414", pos="e,493.79,387.7 565.82,439.99 552.61,429.96 535.47,417.1 520,406 514.31,401.92 508.22,397.65 502.29,393.55"]; "Elemwise{pow,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="198,194", shape=ellipse, style=filled, width=3.1401]; "Elemwise{add,no_inplace} id=1" -> "Elemwise{pow,no_inplace}" [label="0 TensorType(float64, scalar)", lp="384.5,238", pos="e,241.85,210.69 321.39,263.82 310.38,253.1 295.2,239.58 280,230 271.07,224.37 261.08,219.26 251.22,214.78"]; "name=z TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="231,370", shape=box, style=filled, width=3.0556]; "name=z TensorType(float64, scalar)" -> "Elemwise{add,no_inplace} id=1" [label="1 TensorType(float64, scalar)", lp="339.5,326", pos="e,286.05,298.57 235.92,351.97 239.82,341.05 246.27,327.23 256,318 262.13,312.18 269.38,307.25 276.99,303.1"]; sigmoid [fillcolor="#FFAABB", height=0.5, pos="198,106", shape=ellipse, style=filled, width=1.1152]; "Elemwise{pow,no_inplace}" -> sigmoid [label="TensorType(float64, scalar)", lp="276,150", pos="e,198,124.08 198,175.6 198,163.75 198,147.82 198,134.29"]; "val=2 TensorType(int8, scalar)" [fillcolor=limegreen, height=0.5, pos="95,282", shape=box, style=filled, width=2.6389]; "val=2 TensorType(int8, scalar)" -> "Elemwise{pow,no_inplace}" [label="1 TensorType(int8, scalar)", lp="205.5,238", pos="e,159.84,210.97 104.29,263.81 110.72,253.09 120.07,239.56 131,230 136.97,224.77 143.86,220.07 150.9,215.92"]; "TensorType(float64, scalar) id=7" [fillcolor=dodgerblue, height=0.5, pos="198,18", shape=box, style=filled, width=2.7847]; sigmoid -> "TensorType(float64, scalar) id=7" [label="TensorType(float64, scalar)", lp="276,62", pos="e,198,36.084 198,87.597 198,75.746 198,59.817 198,46.292"]; }
\ No newline at end of file
......@@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {
"collapsed": false
},
......
......@@ -51,7 +51,6 @@
<script type="text/javascript">
var path='logreg.dot';
var width = 1200;
var height = 900;
......
......@@ -51,7 +51,6 @@
<script type="text/javascript">
var path='logreg.dot';
var width = 1200;
var height = 900;
......
......@@ -27,6 +27,12 @@ def d3dot(fct, node_colors=None, *args, **kwargs):
return dot_graph
def d3write(fct, path, *args, **kwargs):
dot = d3dot(fct, *args, **kwargs)
with open(path, 'w') as f:
f.write(dot)
def d3print(fct, outfile=None, return_html=False, print_message=True,
width=800, height=600,
*args, **kwargs):
......
......@@ -51,7 +51,6 @@
<script type="text/javascript">
var path='logreg.dot';
var width = 1200;
var height = 900;
......
digraph G { graph [bb="0,0,845,760"]; "DimShuffle{x}" [height=0.5, pos="593,194", shape=ellipse, width=1.8374]; "Elemwise{gt,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="485,106", shape=ellipse, style=filled, width=2.8978]; "DimShuffle{x}" -> "Elemwise{gt,no_inplace}" [label="1 TensorType(float32, (True,))", lp="658.5,150", pos="e,527.05,122.58 584.45,176.15 578.28,165.3 569.09,151.5 558,142 551.48,136.41 543.91,131.5 536.17,127.26"]; "val=0.5 TensorType(float32, scalar)" [fillcolor=limegreen, height=0.5, pos="606,282", shape=box, style=filled, width=3.0278]; "val=0.5 TensorType(float32, scalar)" -> "DimShuffle{x}" [label="TensorType(float32, scalar)", lp="679,238", pos="e,595.58,212.08 603.37,263.6 601.58,251.75 599.17,235.82 597.13,222.29"]; "DimShuffle{x} id=1" [height=0.5, pos="350,566", shape=ellipse, width=2.3721]; "Elemwise{sub,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="461,478", shape=ellipse, style=filled, width=3.0624]; "DimShuffle{x} id=1" -> "Elemwise{sub,no_inplace}" [label="1 TensorType(float64, (True,))", lp="440.5,522", pos="e,385.39,491.23 346.85,547.67 345.89,536.89 346.64,523.35 354,514 360.01,506.36 367.72,500.37 376.25,495.67"]; "name=b TensorType(float64, scalar)" [fillcolor=limegreen, height=0.5, pos="350,654", shape=box, style=filled, width=3.0625]; "name=b TensorType(float64, scalar)" -> "DimShuffle{x} id=1" [label="TensorType(float64, scalar)", lp="428,610", pos="e,350,584.08 350,635.6 350,623.75 350,607.82 350,594.29"]; dot [height=0.5, pos="564,654", shape=ellipse, width=0.75]; "Elemwise{neg,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="564,566", shape=ellipse, style=filled, width=3.0624]; dot -> "Elemwise{neg,no_inplace}" [label="TensorType(float64, vector)", lp="643,610", pos="e,564,584.08 564,635.6 564,623.75 564,607.82 564,594.29"]; "name=x TensorType(float64, matrix)" [fillcolor=limegreen, height=0.5, pos="442,742", shape=box, style=filled, width=3.1181]; "name=x TensorType(float64, matrix)" -> dot [label="0 TensorType(float64, matrix)", lp="579,698", pos="e,541.56,664.07 456.77,724 466.61,713.35 480.21,699.84 494,690 505.81,681.57 519.95,674.09 532.42,668.22"]; "name=w TensorType(float64, vector)" [fillcolor=limegreen, height=0.5, pos="686,742", shape=box, style=filled, width=3.1389]; "name=w TensorType(float64, vector)" -> dot [label="1 TensorType(float64, vector)", lp="760.5,698", pos="e,590.41,658.28 682.35,723.78 679.2,712.78 673.59,698.94 664,690 646.54,673.73 621.08,664.96 600.23,660.27"]; "DimShuffle{x} id=3" [height=0.5, pos="247,390", shape=ellipse, width=2.3721]; "Elemwise{add,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="369,282", shape=ellipse, style=filled, width=3.0624]; "DimShuffle{x} id=3" -> "Elemwise{add,no_inplace}" [label="0 TensorType(int8, (True,))", lp="334,336", pos="e,290.21,294.6 244.34,371.76 242.92,355.96 243.54,332.85 256,318 262.8,309.89 271.45,303.63 280.91,298.8"]; "val=1 TensorType(int8, scalar)" [fillcolor=limegreen, height=0.5, pos="162,478", shape=box, style=filled, width=2.6389]; "val=1 TensorType(int8, scalar)" -> "DimShuffle{x} id=3" [label="TensorType(int8, scalar)", lp="305.5,434", pos="e,242.68,408.18 202.18,459.88 210.48,455 218.62,449.05 225,442 231.23,435.11 235.89,426.19 239.27,417.74"]; "DimShuffle{x} id=4" [height=0.5, pos="85,336", shape=ellipse, width=2.3721]; "val=1 TensorType(int8, scalar)" -> "DimShuffle{x} id=4" [label="TensorType(int8, scalar)", lp="151.5,434", pos="e,78.928,354.24 101.1,459.78 93.551,455.18 86.813,449.36 82,442 66.938,418.97 70.674,386.61 76.281,363.92"]; "Elemwise{true_div,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="369,194", shape=ellipse, style=filled, width=3.5561]; "DimShuffle{x} id=4" -> "Elemwise{true_div,no_inplace}" [label="0 TensorType(int8, (True,))", lp="172,282", pos="e,277.79,206.71 82.19,317.91 80.604,301.99 81.056,278.59 94,264 117.19,237.85 199.28,219.48 267.79,208.3"]; "Elemwise{neg,no_inplace}" -> "Elemwise{sub,no_inplace}" [label="0 TensorType(float64, vector)", lp="629.5,522", pos="e,501.82,494.86 555.83,547.71 550.08,536.96 541.53,523.42 531,514 525.02,508.65 518.07,503.9 510.93,499.77"]; "Elemwise{exp,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="461,390", shape=ellipse, style=filled, width=3.0624]; "Elemwise{sub,no_inplace}" -> "Elemwise{exp,no_inplace}" [label="TensorType(float64, vector)", lp="540,434", pos="e,461,408.08 461,459.6 461,447.75 461,431.82 461,418.29"]; "Elemwise{exp,no_inplace}" -> "Elemwise{add,no_inplace}" [label="1 TensorType(float64, vector)", lp="526.5,336", pos="e,394.8,299.6 452.54,372.02 444.46,356.82 431.28,334.48 416,318 412.01,313.7 407.43,309.56 402.74,305.73"]; "Elemwise{add,no_inplace}" -> "Elemwise{true_div,no_inplace}" [label="1 TensorType(float64, vector)", lp="453.5,238", pos="e,369,212.08 369,263.6 369,251.75 369,235.82 369,222.29"]; "Elemwise{true_div,no_inplace}" -> "Elemwise{gt,no_inplace}" [label="0 TensorType(float64, vector)", lp="469.5,150", pos="e,419.59,120.09 370.47,175.87 372.26,164.9 376.22,151.07 385,142 392.17,134.59 401,128.74 410.42,124.14"]; "TensorType(int8, vector)" [fillcolor=dodgerblue, height=0.5, pos="485,18", shape=box, style=filled, width=2.1736]; "Elemwise{gt,no_inplace}" -> "TensorType(int8, vector)" [label="TensorType(int8, vector)", lp="555.5,62", pos="e,485,36.084 485,87.597 485,75.746 485,59.817 485,46.292"]; }
digraph G { graph [bb="0,0,763,388"]; "DimShuffle{x,0}" [height=0.5, pos="600,282", shape=ellipse, width=2.0339]; "Elemwise{add,no_inplace}" [fillcolor="#FFAABB", height=0.5, pos="474,194", shape=ellipse, style=filled, width=3.0624]; "DimShuffle{x,0}" -> "Elemwise{add,no_inplace}" [label="1 TensorType(float64, row)", lp="648,238", pos="e,516.94,210.65 587.13,264.21 578.18,253.39 565.47,239.59 552,230 544.1,224.38 535.16,219.35 526.22,214.97"]; "name=b TensorType(float64, vector)" [fillcolor=limegreen, height=0.5, pos="607,370", shape=box, style=filled, width=3.0903]; "name=b TensorType(float64, vector)" -> "DimShuffle{x,0}" [label="TensorType(float64, vector)", lp="684,326", pos="e,601.39,300.08 605.58,351.6 604.62,339.75 603.32,323.82 602.22,310.29"]; dot [height=0.5, pos="362,282", shape=ellipse, width=0.75]; dot -> "Elemwise{add,no_inplace}" [label="0 TensorType(float64, matrix)", lp="467,238", pos="e,414.72,209.23 365,263.89 367.74,252.92 372.81,239.1 382,230 388.76,223.31 396.93,217.87 405.58,213.46"]; "name=X TensorType(float64, matrix)" [fillcolor=limegreen, height=0.5, pos="114,370", shape=box, style=filled, width=3.1667]; "name=X TensorType(float64, matrix)" -> dot [label="0 TensorType(float64, matrix)", lp="273,326", pos="e,335.37,285 134.04,351.84 148.22,340.57 168.18,326.4 188,318 233.06,298.9 289.18,290.03 325.29,286.04"]; "name=W TensorType(float64, matrix)" [fillcolor=limegreen, height=0.5, pos="362,370", shape=box, style=filled, width=3.2014]; "name=W TensorType(float64, matrix)" -> dot [label="1 TensorType(float64, matrix)", lp="447,326", pos="e,362,300.08 362,351.6 362,339.75 362,323.82 362,310.29"]; Softmax [height=0.5, pos="474,106", shape=ellipse, width=1.1472]; "Elemwise{add,no_inplace}" -> Softmax [label="TensorType(float64, matrix)", lp="554,150", pos="e,474,124.08 474,175.6 474,163.75 474,147.82 474,134.29"]; "TensorType(float64, matrix) id=6" [fillcolor=dodgerblue, height=0.5, pos="474,18", shape=box, style=filled, width=2.8403]; Softmax -> "TensorType(float64, matrix) id=6" [label="TensorType(float64, matrix)", lp="554,62", pos="e,474,36.084 474,87.597 474,75.746 474,59.817 474,46.292"]; }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论