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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
package tensor
import (
"math"
"github.com/chewxy/math32"
"github.com/pkg/errors"
)
func (t *Dense) multiSVDNorm(rowAxis, colAxis int) (retVal *Dense, err error) {
if rowAxis > colAxis {
rowAxis--
}
dims := t.Dims()
if retVal, err = t.RollAxis(colAxis, dims, true); err != nil {
return
}
if retVal, err = retVal.RollAxis(rowAxis, dims, true); err != nil {
return
}
// manual, since SVD only works on matrices. In the future, this needs to be fixed when gonum's lapack works for float32
// TODO: SVDFuture
switch dims {
case 2:
retVal, _, _, err = retVal.SVD(false, false)
case 3:
toStack := make([]*Dense, retVal.Shape()[0])
for i := 0; i < retVal.Shape()[0]; i++ {
var sliced, ithS *Dense
if sliced, err = sliceDense(retVal, ss(i)); err != nil {
return
}
if ithS, _, _, err = sliced.SVD(false, false); err != nil {
return
}
toStack[i] = ithS
}
retVal, err = toStack[0].Stack(0, toStack[1:]...)
return
default:
err = errors.Errorf("multiSVDNorm for dimensions greater than 3")
}
return
}
// Norm returns the p-ordered norm of the *Dense, given the axes.
//
// This implementation is directly adapted from Numpy, which is licenced under a BSD-like licence, and can be found here: https://docs.scipy.org/doc/numpy-1.9.1/license.html
func (t *Dense) Norm(ord NormOrder, axes ...int) (retVal *Dense, err error) {
var ret Tensor
var ok bool
var abs, norm0, normN interface{}
var oneOverOrd interface{}
switch t.t {
case Float64:
abs = math.Abs
norm0 = func(x float64) float64 {
if x != 0 {
return 1
}
return 0
}
normN = func(x float64) float64 {
return math.Pow(math.Abs(x), float64(ord))
}
oneOverOrd = float64(1) / float64(ord)
case Float32:
abs = math32.Abs
norm0 = func(x float32) float32 {
if x != 0 {
return 1
}
return 0
}
normN = func(x float32) float32 {
return math32.Pow(math32.Abs(x), float32(ord))
}
oneOverOrd = float32(1) / float32(ord)
default:
err = errors.Errorf("Norms only works on float types")
return
}
dims := t.Dims()
// simple case
if len(axes) == 0 {
if ord.IsUnordered() || (ord.IsFrobenius() && dims == 2) || (ord == Norm(2) && dims == 1) {
backup := t.AP
ap := makeAP(1)
defer ap.zero()
ap.unlock()
ap.SetShape(t.Size())
ap.lock()
t.AP = ap
if ret, err = Dot(t, t); err != nil { // returns a scalar
err = errors.Wrapf(err, opFail, "Norm-0")
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "Norm-0")
}
switch t.t {
case Float64:
retVal.SetF64(0, math.Sqrt(retVal.GetF64(0)))
case Float32:
retVal.SetF32(0, math32.Sqrt(retVal.GetF32(0)))
}
t.AP = backup
return
}
axes = make([]int, dims)
for i := range axes {
axes[i] = i
}
}
switch len(axes) {
case 1:
cloned := t.Clone().(*Dense)
switch {
case ord.IsUnordered() || ord == Norm(2):
if ret, err = Square(cloned); err != nil {
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "UnorderedNorm-1")
}
if retVal, err = retVal.Sum(axes...); err != nil {
return
}
if ret, err = Sqrt(retVal); err != nil {
return
}
return assertDense(ret)
case ord.IsInf(1):
if ret, err = cloned.Apply(abs); err != nil {
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "InfNorm-1")
}
return retVal.Max(axes...)
case ord.IsInf(-1):
if ret, err = cloned.Apply(abs); err != nil {
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "-InfNorm-1")
}
return retVal.Min(axes...)
case ord == Norm(0):
if ret, err = cloned.Apply(norm0); err != nil {
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "Norm-0")
}
return retVal.Sum(axes...)
case ord == Norm(1):
if ret, err = cloned.Apply(abs); err != nil {
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "Norm-1")
}
return retVal.Sum(axes...)
default:
if ret, err = cloned.Apply(normN); err != nil {
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "Norm-N")
}
if retVal, err = retVal.Sum(axes...); err != nil {
return
}
return retVal.PowScalar(oneOverOrd, true)
}
case 2:
rowAxis := axes[0]
colAxis := axes[1]
// checks
if rowAxis < 0 {
return nil, errors.Errorf("Row Axis %d is < 0", rowAxis)
}
if colAxis < 0 {
return nil, errors.Errorf("Col Axis %d is < 0", colAxis)
}
if rowAxis == colAxis {
return nil, errors.Errorf("Duplicate axes found. Row Axis: %d, Col Axis %d", rowAxis, colAxis)
}
cloned := t.Clone().(*Dense)
switch {
case ord == Norm(2):
// svd norm
if retVal, err = t.multiSVDNorm(rowAxis, colAxis); err != nil {
return nil, errors.Wrapf(err, opFail, "MultiSVDNorm, case 2 with Ord == Norm(2)")
}
dims := retVal.Dims()
return retVal.Max(dims - 1)
case ord == Norm(-2):
// svd norm
if retVal, err = t.multiSVDNorm(rowAxis, colAxis); err != nil {
return nil, errors.Wrapf(err, opFail, "MultiSVDNorm, case 2 with Ord == Norm(-2)")
}
dims := retVal.Dims()
return retVal.Min(dims - 1)
case ord == Norm(1):
if colAxis > rowAxis {
colAxis--
}
if ret, err = cloned.Apply(abs); err != nil {
return nil, errors.Wrapf(err, opFail, "Apply abs in Norm. ord == Norm(1")
}
if retVal, err = assertDense(ret); err != nil {
return nil, errors.Wrapf(err, opFail, "Norm-1, axis=2")
}
if retVal, err = retVal.Sum(rowAxis); err != nil {
return
}
return retVal.Max(colAxis)
case ord == Norm(-1):
if colAxis > rowAxis {
colAxis--
}
if ret, err = cloned.Apply(abs); err != nil {
return
}
if retVal, err = assertDense(ret); err != nil {
return nil, errors.Wrapf(err, opFail, "Norm-(-1), axis=2")
}
if retVal, err = retVal.Sum(rowAxis); err != nil {
return
}
return retVal.Min(colAxis)
case ord == Norm(0):
return nil, errors.Errorf("Norm of order 0 undefined for matrices")
case ord.IsInf(1):
if rowAxis > colAxis {
rowAxis--
}
if ret, err = cloned.Apply(abs); err != nil {
return
}
if retVal, err = assertDense(ret); err != nil {
return nil, errors.Wrapf(err, opFail, "InfNorm, axis=2")
}
if retVal, err = retVal.Sum(colAxis); err != nil {
return nil, errors.Wrapf(err, "Sum in infNorm")
}
return retVal.Max(rowAxis)
case ord.IsInf(-1):
if rowAxis > colAxis {
rowAxis--
}
if ret, err = cloned.Apply(abs); err != nil {
return
}
if retVal, err = assertDense(ret); err != nil {
return nil, errors.Wrapf(err, opFail, "-InfNorm, axis=2")
}
if retVal, err = retVal.Sum(colAxis); err != nil {
return nil, errors.Wrapf(err, opFail, "Sum with InfNorm")
}
return retVal.Min(rowAxis)
case ord.IsUnordered() || ord.IsFrobenius():
if ret, err = cloned.Apply(abs); err != nil {
return
}
if retVal, ok = ret.(*Dense); !ok {
return nil, errors.Errorf(opFail, "Frobenius Norm, axis = 2")
}
if ret, err = Square(retVal); err != nil {
return
}
if retVal, err = assertDense(ret); err != nil {
return nil, errors.Wrapf(err, opFail, "Norm-0, axis=2")
}
if retVal, err = retVal.Sum(axes...); err != nil {
return
}
if ret, err = Sqrt(retVal); err != nil {
return
}
return assertDense(ret)
case ord.IsNuclear():
// svd norm
if retVal, err = t.multiSVDNorm(rowAxis, colAxis); err != nil {
return
}
return retVal.Sum(len(t.Shape()) - 1)
case ord == Norm(0):
err = errors.Errorf("Norm order 0 undefined for matrices")
return
default:
return nil, errors.Errorf("Not yet implemented: Norm for Axes %v, ord %v", axes, ord)
}
default:
err = errors.Errorf(dimMismatch, 2, len(axes))
return
}
panic("Unreachable")
}
|