forked from databricks/tensorframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevTensorFlow.sc
More file actions
105 lines (90 loc) · 3.2 KB
/
DevTensorFlow.sc
File metadata and controls
105 lines (90 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// -*- scala -*-
// We don't seem to be able to get the names directly,
// but through python we can actually get all of them
// So initialize a python shell along the way to check the graph definition
// An `Output` is a symbolic representation of the Tensor
// There is really no `Input` as these are inferred from the knowledge of the graph
// REF: https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Operation
import $exec.DevDataSet, DevDataSet._
import scala.collection.JavaConverters._
import org.{tensorflow => tf}
import org.tensorflow.{framework => tfrm}
import org.tensorflow.framework.{
AttrValue, DataType => ProtoDataType,
GraphDef, NodeDef, TensorProto, TensorShapeProto
}
println(s"TensorFlow version: ${tf.TensorFlow.version}")
object TFOps {
type GraphDef = Array[Byte]
implicit def gdefbytes2gdef(bytes: GraphDef): tfrm.GraphDef = {
tfrm.GraphDef.parseFrom(bytes)
}
case class GraphFunction(
gdef: GraphDef,
inputNames: Seq[String],
outputNames: Seq[String]
) {
@transient private lazy val graphDef = tfrm.GraphDef.parseFrom(gdef)
@transient private lazy val nodeDict = graphDef.getNodeList.asScala.map {
node => node.getName -> node
}.toMap
def node(name: String): tfrm.NodeDef = {
nodeDict.getOrElse(name, throw new IllegalArgumentException("no op found"))
}
def getNodeDict = nodeDict
}
object GraphFunction {
def apply(fp: Path, inputNames: Seq[String], outputNames: Seq[String]): GraphFunction = {
new GraphFunction(Files.readAllBytes(fp), inputNames, outputNames)
}
}
case class IsolatedSession(sess: tf.Session, g: tf.Graph) {
def op(name: String): tf.Operation = {
val op = g.operation(name)
require(null != op, s"Graph operation [$name] not fouhnd")
op
}
def op(elem: tf.Operation): tf.Operation = elem
def op(tnsr: tf.Output): tf.Operation = tnsr.op
def importGraphFunction(gfn: GraphFunction, name: Option[String] = None) = {
val bytes = gfn.gdef.toByteArray
val getNodeOp = name match {
case Some(scope) =>
g.importGraphDef(bytes, scope)
val impl: String => tf.Operation = { vn => g.operation(s"$scope/$vn") }
impl
case None =>
g.importGraphDef(bytes)
g.operation _
}
val feeds = gfn.inputNames.map(getNodeOp)
val fetches = gfn.outputNames.map(getNodeOp)
feeds -> fetches
}
def showOp(name: String): Unit = {
val op = g.operation(name)
require(null != op, s"Graph operation [$name] not fouhnd")
tfx.show(op)
}
}
object tfx {
def show(op: tf.Operation): Unit = {
println(s"name: ${op.name}, #outputs: ${op.numOutputs}, type: ${op.`type`}")
(0 until op.numOutputs).foreach { idx =>
val tnsrOut = op.output(idx)
println(tnsrOut.shape, tnsrOut.dataType)
}
}
}
object IsolatedSession {
def apply(): IsolatedSession = apply(new tf.Graph())
def apply(g: tf.Graph): IsolatedSession = {
new IsolatedSession(new tf.Session(g), g)
}
def apply(gfn: GraphFunction): IsolatedSession = {
val g = new tf.Graph()
g.importGraphDef(gfn.gdef.toByteArray)
apply(g)
}
}
}