def ConfigPats(ss): dt = ss.Pats sch = etable.Schema( [etable.Column("Name", etensor.STRING, go.nil, go.nil), etable.Column("Input", etensor.FLOAT32, go.Slice_int([4, 5]), go.Slice_string(["Y", "X"])), etable.Column("Output", etensor.FLOAT32, go.Slice_int([4, 5]), go.Slice_string(["Y", "X"]))] ) dt.SetFromSchema(sch, 3) ss.Pats.SetMetaData("name", "Pats") ss.Pats.SetMetaData("desc", "Training patterns") patgen.PermutedBinaryRows(dt.Cols[1], 6, 1, 0) patgen.PermutedBinaryRows(dt.Cols[2], 6, 1, 0) cn = etensor.String(dt.Cols[0]) cn.Values.copy(["any", "baker", "cheese"])
def Numpy(ss): """ test conversions to / from numpy """ dt = ss.Pats print("\n\n##############################") print("to / from numpy") etf = etensor.Float32(dt.Cols[1]) npf = pyet.etensor_to_numpy(etf) print(npf) ctf = pyet.numpy_to_etensor(npf) print(ctf) etu32 = etensor.NewUint32(go.Slice_int([3,4,5]), go.nil, go.nil) sz = etf.Len() for i in range(sz): etu32.Values[i] = int(etf.Values[i]) print(etu32) npu32 = pyet.etensor_to_numpy(etu32) print(npu32) ctu32 = pyet.numpy_to_etensor(npu32) print(ctu32) pyet.copy_etensor_to_numpy(npu32, etu32) pyet.copy_numpy_to_etensor(etu32, npu32) ets = etensor.String(dt.Cols[0]) nps = pyet.etensor_to_numpy(ets) print(nps) cts = pyet.numpy_to_etensor(nps) print(cts) pyet.copy_etensor_to_numpy(nps, ets) pyet.copy_numpy_to_etensor(ets, nps) etb = etensor.NewBits(go.Slice_int([3,4,5]), go.nil, go.nil) sz = etb.Len() for i in range(sz): etb.Set1D(i, erand.BoolProb(.2, -1)) print(etb) npb = pyet.etensor_to_numpy(etb) print(npb) ctb = pyet.numpy_to_etensor(npb) print(ctb) pyet.copy_etensor_to_numpy(npb, etb) pyet.copy_numpy_to_etensor(etb, npb)
def etensor_to_numpy(et): """ returns a numpy ndarray constructed from the given etensor.Tensor. data is copied into the numpy ndarray -- it is not a view. """ nar = 0 if et.DataType() == etensor.UINT8: nar = np.array(etensor.Uint8(et).Values, dtype=np.uint8) elif et.DataType() == etensor.INT8: nar = np.array(etensor.Int8(et).Values, dtype=np.int8) elif et.DataType() == etensor.UINT16: nar = np.array(etensor.Uint16(et).Values, dtype=np.uint16) elif et.DataType() == etensor.INT16: nar = np.array(etensor.Int16(et).Values, dtype=np.int16) elif et.DataType() == etensor.UINT32: nar = np.array(etensor.Uint32(et).Values, dtype=np.uint32) elif et.DataType() == etensor.INT32: nar = np.array(etensor.Int32(et).Values, dtype=np.int32) elif et.DataType() == etensor.UINT64: nar = np.array(etensor.Uint64(et).Values, dtype=np.uint64) elif et.DataType() == etensor.INT64: nar = np.array(etensor.Int64(et).Values, dtype=np.int64) elif et.DataType() == etensor.FLOAT32: nar = np.array(etensor.Float32(et).Values, dtype=np.float32) elif et.DataType() == etensor.FLOAT64: nar = np.array(etensor.Float64(et).Values, dtype=np.float64) elif et.DataType() == etensor.STRING: nar = np.array(etensor.String(et).Values) elif et.DataType() == etensor.INT: nar = np.array(etensor.Int(et).Values, dtype=np.intc) elif et.DataType() == etensor.BOOL: etb = etensor.Bits(et) sz = etb.Len() nar = np.zeros(sz, dtype=np.bool_) for i in range(sz): nar[i] = etb.Value1D(i) else: raise TypeError("tensor with type %s cannot be converted" % (et.DataType().String())) return 0 # there does not appear to be a way to set the shape at the same time as initializing return nar.reshape(et.Shapes())
def copy_numpy_to_etensor(et, nar): """ copies data from numpy ndarray (nar, source) to existing etensor.Tensor (et, dest) """ narf = np.reshape(nar, -1) etv = et if et.DataType() == etensor.UINT8: etv = etensor.Uint8(et).Values elif et.DataType() == etensor.INT8: etv = etensor.Int8(et).Values elif et.DataType() == etensor.UINT16: etv = etensor.Uint16(et).Values elif et.DataType() == etensor.INT16: etv = etensor.Int16(et).Values elif et.DataType() == etensor.UINT32: etv = etensor.Uint32(et).Values elif et.DataType() == etensor.INT32: etv = etensor.Int32(et).Values elif et.DataType() == etensor.UINT64: etv = etensor.Uint64(et).Values elif et.DataType() == etensor.INT64: etv = etensor.Int64(et).Values elif et.DataType() == etensor.FLOAT32: etv = etensor.Float32(et).Values elif et.DataType() == etensor.FLOAT64: etv = etensor.Float64(et).Values elif et.DataType() == etensor.STRING: etv = etensor.String(et).Values elif et.DataType() == etensor.INT: etv = etensor.Int(et).Values elif et.DataType() == etensor.BOOL: etb = etensor.Bits(et) sz = min(etb.Len(), len(narf)) for i in range(sz): narf[i] = etb.Value1D(i) return else: raise TypeError("tensor with type %s cannot be copied" % (et.DataType().String())) return 0 etv.copy(narf) # go slice copy, not python copy = clone