mirror of
https://github.com/wassname/keras-js.git
synced 2026-10-30 13:20:13 +08:00
implement UpSampling1D/2D/3D layers, with tests
This commit is contained in:
1 parent
032e290607
commit
9b07e511bf
15 files changed
+1185
-6
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import Layer from '../../engine/Layer'
|
||||
import Tensor from '../../Tensor'
|
||||
import ops from 'ndarray-ops'
|
||||
|
||||
/**
|
||||
* UpSampling2D layer class
|
||||
*/
|
||||
export default class UpSampling2D extends Layer {
|
||||
/**
|
||||
* Creates a UpSampling2D activation layer
|
||||
* @param {number} size - upsampling factor
|
||||
*/
|
||||
constructor (size = [2, 2], attrs = {}) {
|
||||
super(attrs)
|
||||
const {
|
||||
dimOrdering = 'tf'
|
||||
} = attrs
|
||||
|
||||
this.size = size
|
||||
this.dimOrdering = dimOrdering
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for layer computational logic
|
||||
* @param {Tensor} x
|
||||
* @returns {Tensor} x
|
||||
*/
|
||||
call = x => {
|
||||
// convert to tf ordering
|
||||
if (this.dimOrdering === 'th') {
|
||||
x.tensor = x.tensor.transpose(1, 2, 0)
|
||||
}
|
||||
|
||||
const inputShape = x.tensor.shape
|
||||
const outputShape = [
|
||||
inputShape[0] * this.size[0],
|
||||
inputShape[1] * this.size[1],
|
||||
inputShape[2]
|
||||
]
|
||||
let y = new Tensor([], outputShape)
|
||||
for (let i = 0; i < this.size[0]; i++) {
|
||||
for (let j = 0; j < this.size[1]; j++) {
|
||||
ops.assign(
|
||||
y.tensor.lo(i, j, 0).step(this.size[0], this.size[1], 1),
|
||||
x.tensor
|
||||
)
|
||||
}
|
||||
}
|
||||
x.tensor = y.tensor
|
||||
|
||||
// convert back to th ordering if necessary
|
||||
if (this.dimOrdering === 'th') {
|
||||
x.tensor = x.tensor.transpose(2, 0, 1)
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user