Skip to content

Instantly share code, notes, and snippets.

@strubell
Created February 12, 2014 16:38
Show Gist options
  • Save strubell/8959278 to your computer and use it in GitHub Desktop.
Save strubell/8959278 to your computer and use it in GitHub Desktop.
maxtrix, vector left multiply vs. left multiply + sum + max
def leftMultiplyAndSumAndMax(t: Tensor1, v: DenseTensor1): (DenseTensor1, Double) = {
assert(dim1 == t.dim1, "Dimensions don't match: " + dim1 + " " + t.dim1)
val myDim2 = dim2
val newT = v.copy
val newArray = newT.asArray
var max = Double.MinValue
var currentVal = 0.0
t match {
case t: DenseTensor =>
val tArr = t.asArray
var row = 0
while (row < tArr.length-1) {
val v = tArr(row)
val offset = row * myDim2
var col = 0
while (col < myDim2) {
newArray(col) += (apply(offset + col) * v)
col += 1
}
row += 1
}
/* Do final iteration separately since it's the only one where we need max logic */
val v = tArr(row)
val offset = row * myDim2
var col = 0
while (col < myDim2) {
newArray(col) += (apply(offset + col) * v)
currentVal = newArray(col)
if(currentVal > max){
max = currentVal
}
col += 1
}
case t: SparseBinaryTensor =>
val tActiveDomainSize = t.activeDomainSize
val tIndices = t._indices
var ti = 0
while (ti < tActiveDomainSize-1) {
val row = tIndices(ti)
val offset = row * myDim2
var col = 0
while (col < myDim2) {
newArray(col) += apply(offset + col)
currentVal = newArray(col)
col += 1
}
ti += 1
}
/* Do final iteration separately since it's the only one where we need max logic */
val row = tIndices(ti)
val offset = row * myDim2
var col = 0
while (col < myDim2) {
newArray(col) += apply(offset + col)
currentVal = newArray(col)
if(currentVal > max)
max = currentVal
col += 1
}
case t: SparseIndexedTensor =>
val tActiveDomainSize = t.activeDomainSize
val tIndices = t._indices
val tValues = t._values
var ti = 0
while (ti < tActiveDomainSize-1) {
val row = tIndices(ti)
val offset = row * myDim2
val v = tValues(ti)
var col = 0
while (col < myDim2) {
newArray(col) += (apply(offset + col) * v)
col += 1
}
ti += 1
}
/* Do final iteration separately since it's the only one where we need max logic */
val row = tIndices(ti)
val offset = row * myDim2
val v = tValues(ti)
var col = 0
while (col < myDim2) {
newArray(col) += (apply(offset + col) * v)
currentVal = newArray(col)
if(currentVal > max)
max = currentVal
col += 1
}
case _ =>
throw new Error("tensor type neither dense nor sparse: " + t.getClass.getName)
}
(newT, max)
}
def leftMultiply(t: Tensor1): Tensor1 = {
assert(dim1 == t.dim1, "Dimensions don't match: " + dim1 + " " + t.dim1)
val myDim2 = dim2
val newT = new DenseTensor1(dim2)
val newArray = newT.asArray
t match {
case t: DenseTensor =>
val tArr = t.asArray
var row = 0
while (row < tArr.length) {
val v = tArr(row)
val offset = row * myDim2
var col = 0
while (col < myDim2) {
newArray(col) += (apply(offset + col) * v)
col += 1
}
row += 1
}
case t: SparseBinaryTensor =>
val tActiveDomainSize = t.activeDomainSize
val tIndices = t._indices
var ti = 0
while (ti < tActiveDomainSize) {
val row = tIndices(ti)
val offset = row * myDim2
var col = 0
while (col < myDim2) {
newArray(col) += apply(offset + col)
col += 1
}
ti += 1
}
case t: SparseIndexedTensor =>
val tActiveDomainSize = t.activeDomainSize
val tIndices = t._indices
val tValues = t._values
var ti = 0
while (ti < tActiveDomainSize) {
val row = tIndices(ti)
val offset = row * myDim2
val v = tValues(ti)
var col = 0
while (col < myDim2) {
newArray(col) += (apply(offset + col) * v)
col += 1
}
ti += 1
}
case _ =>
throw new Error("tensor type neither dense nor sparse: " + t.getClass.getName)
}
newT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment