* Helper function to check the dtype and shape compatibility of a feed value.
(key: SymbolicTensor, val: Tensor)
| 26 | * Helper function to check the dtype and shape compatibility of a feed value. |
| 27 | */ |
| 28 | function assertFeedCompatibility(key: SymbolicTensor, val: Tensor): Tensor { |
| 29 | // Check dtype compatibility. |
| 30 | if (key.dtype == null || key.dtype === val.dtype) { |
| 31 | // a. If types match, return val tensor as is. |
| 32 | return val; |
| 33 | } |
| 34 | try { |
| 35 | // b. Attempt to convert to expected type. |
| 36 | return cast(val, key.dtype); |
| 37 | } catch (err) { |
| 38 | // c. If conversion fails, return helpful error. |
| 39 | throw new ValueError( |
| 40 | `The dtype of the feed (${val.dtype}) can not be cast to the dtype ` + |
| 41 | `of the key '${key.name}' (${key.dtype}).`); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * A concrete Tensor value for a symbolic tensor as the key. |