-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyftBackendTensorFlow.js
39 lines (33 loc) · 1.01 KB
/
SyftBackendTensorFlow.js
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
const TF_COMMAND_MAP = new Map([
[COMMAND_ADD, 'add'],
[COMMAND_MUL, 'mul'],
[COMMAND_MATMUL, 'matMul'],
[COMMAND_DIV, 'div'],
])
class SyftBackendTensorFlow extends SyftBackend {
/**
* Convert to TF tensor
* @param value
* @returns {tf.Tensor}
*/
toTensor(value) {
if (value instanceof SyftTorchTensor) {
return tf.tensor(value.data, value.shape)
} else if (typeof value === "number") {
return tf.tensor(value)
} else if (value instanceof tf.Tensor) {
return value
} else {
throw new Error(`Failed to make tensor from value ${value}`)
}
}
toSyftTensor(tensorId, tensor) {
const data = tensor.dataSync()
const shape = tensor.shape
return new SyftTorchTensor(tensorId, data, shape)
}
executeOp(command, tensor, operand) {
const tfOp = TF_COMMAND_MAP.get(command)
return tensor[tfOp](operand)
}
}