Class: Tensorflow::Graph::OperationAttr

Inherits:
Object
  • Object
show all
Defined in:
lib/tensorflow/graph/operation_attr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, name, metadata) ⇒ OperationAttr

Returns a new instance of OperationAttr.



6
7
8
9
10
# File 'lib/tensorflow/graph/operation_attr.rb', line 6

def initialize(operation, name, )
  @operation = operation
  @name = name
  @metadata = 
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



4
5
6
# File 'lib/tensorflow/graph/operation_attr.rb', line 4

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/tensorflow/graph/operation_attr.rb', line 4

def name
  @name
end

#operationObject (readonly)

Returns the value of attribute operation.



4
5
6
# File 'lib/tensorflow/graph/operation_attr.rb', line 4

def operation
  @operation
end

Instance Method Details

#boolObject



40
41
42
43
44
45
46
# File 'lib/tensorflow/graph/operation_attr.rb', line 40

def bool
  pointer = ::FFI::MemoryPointer.new(:uchar)
  Status.check do |status|
    FFI.TF_OperationGetAttrBool(self.operation, self.name, pointer, status)
  end
  Boolean(pointer.read_uchar)
end

#dtypeObject



48
49
50
51
52
53
54
55
# File 'lib/tensorflow/graph/operation_attr.rb', line 48

def dtype
  pointer = ::FFI::MemoryPointer.new(FFI::DataType.native_type)
  Status.check do |status|
    FFI.TF_OperationGetAttrType(self.operation, self.name, pointer, status)
  end
  value = pointer.read(FFI::DataType.native_type)
  FFI::DataType[value]
end

#dtype_listObject



57
58
59
60
61
62
63
64
65
# File 'lib/tensorflow/graph/operation_attr.rb', line 57

def dtype_list
  pointer = ::FFI::MemoryPointer.new(FFI::DataType.native_type, self.[:list_size])
  Status.check do |status|
    FFI.TF_OperationGetAttrTypeList(self.operation, self.name, pointer, self.[:list_size], status)
  end
  pointer.read_array_of_type(FFI::DataType.native_type, :read_uint32, self.[:list_size]).map do |value|
    FFI::DataType[value]
  end
end

#floatObject



67
68
69
70
71
72
73
# File 'lib/tensorflow/graph/operation_attr.rb', line 67

def float
  pointer = ::FFI::MemoryPointer.new(:float)
  Status.check do |status|
    FFI.TF_OperationGetAttrFloat(self.operation, self.name, pointer, status)
  end
  pointer.read_float
end

#funcObject



75
76
77
# File 'lib/tensorflow/graph/operation_attr.rb', line 75

def func
  self.proto.func.name
end

#intObject



79
80
81
82
83
84
85
# File 'lib/tensorflow/graph/operation_attr.rb', line 79

def int
  pointer = ::FFI::MemoryPointer.new(:int64)
  Status.check do |status|
    FFI.TF_OperationGetAttrInt(self.operation, self.name, pointer, status)
  end
  pointer.read_int
end

#list?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/tensorflow/graph/operation_attr.rb', line 12

def list?
  self.[:is_list] > 0
end

#protoObject



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tensorflow/graph/operation_attr.rb', line 136

def proto
  buffer_ptr = FFI.TF_NewBuffer
  Status.check do |status|
    FFI.TF_OperationGetAttrValueProto(self.operation, self.name, buffer_ptr, status)
  end
  buffer = FFI::Buffer.new(buffer_ptr)
  string = buffer[:data].read_string(buffer[:length])
  AttrValue.decode(string)
ensure
  FFI.TF_DeleteBuffer(buffer)
end

#shapeObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tensorflow/graph/operation_attr.rb', line 87

def shape
  size = self.[:total_size]
  if size == -1
    []
  else
    pointer = ::FFI::MemoryPointer.new(:int64, size)
    Status.check do |status|
      FFI.TF_OperationGetAttrShape(self.operation, self.name, pointer, size, status)
    end
    pointer.read_array_of_int64(size)
  end
end

#shape_listObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tensorflow/graph/operation_attr.rb', line 100

def shape_list
  total_size = self.[:total_size]
  storage_ptr = ::FFI::MemoryPointer.new(:int64, total_size)
  dims_pointer = ::FFI::MemoryPointer.new(:pointer, self.[:list_size])
  num_dims_pointer = ::FFI::MemoryPointer.new(:int, self.[:list_size])
  Status.check do |status|
    FFI.TF_OperationGetAttrShapeList(self.operation, self.name,
                                     dims_pointer, num_dims_pointer,
                                     self.[:list_size],
                                     storage_ptr, total_size, status)
  end

  num_dims = num_dims_pointer.read_array_of_int(self.[:list_size])
  num_dims.map.with_index do |dims, i|
    shape_pointer = dims_pointer[i].read_pointer
    shape_pointer.read_array_of_int64(dims)
  end
end

#stringObject



119
120
121
122
123
124
125
126
# File 'lib/tensorflow/graph/operation_attr.rb', line 119

def string
  size = self.[:total_size]
  pointer = ::FFI::MemoryPointer.new(:string, size)
  Status.check do |status|
    FFI.TF_OperationGetAttrString(self.operation, self.name, pointer, size, status)
  end
  pointer.read_string
end

#tensorObject



128
129
130
131
132
133
134
# File 'lib/tensorflow/graph/operation_attr.rb', line 128

def tensor
  pointer = ::FFI::MemoryPointer.new(:pointer)
  Status.check do |status|
    FFI.TF_OperationGetAttrTensor(self.operation, self.name, pointer, status)
  end
  Tensor.from_pointer(pointer.read_pointer)
end

#to_sObject



148
149
150
# File 'lib/tensorflow/graph/operation_attr.rb', line 148

def to_s
  "#{self.name}: #{self.value}"
end

#valueObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tensorflow/graph/operation_attr.rb', line 16

def value
  case self.[:type]
    when :bool
      self.list? ? self.bool_list : self.bool
    when :int
      self.list? ? self.int_list : self.int
    when :float
      self.list? ? self.float_list : self.float
      self.float
    when :func
      self.list? ? self.func_list : self.func
    when :shape
      self.list? ? self.shape_list : self.shape
    when :string
      self.list? ? self.string_list : self.string
    when :tensor
      self.list? ? self.tensor_list : self.tensor
    when :type
      self.list? ? self.dtype_list : self.dtype
    else
      raise(Error::UnimplementedError, "Unsupported attribute. #{self.name} - #{self.[:type]}")
  end
end