Skip to content

Instantly share code, notes, and snippets.

@mulloymorrow
Created June 22, 2020 23:01
Show Gist options
  • Save mulloymorrow/8f97592c79afa5f71594208bcd760bc4 to your computer and use it in GitHub Desktop.
Save mulloymorrow/8f97592c79afa5f71594208bcd760bc4 to your computer and use it in GitHub Desktop.

What is numpy doing to get this ouput?

import numpy as np
tensor_content = b"\314Lr\3333\304\357?\025\367\032\340\354\214\357?\306\024[\273\216Y\357?}%\344e\232)\357?\341%\337J\247\374\356?W\371<Q^\322\356?\217\271\2520v\252\356?\350u\254\257\260\204\356?\324\330\327\210\330`\356?\031e\364\311\277>\356??&k\215>\036\356?T+\223\3671\377\355?\345\246\245g{\341\355?\310\203z\320\377\304\355?w\276=0\247\251\355?\307\365~ \\\217\355?\275\353\223y\vv\355?\307\360q\005\244]\355?2\233\000?\026F\355?\3607\217\033T/\355?\347\025\225\334P\031\355?\334\312E\350\000\004\355?\032t\315\247Y\357\354?Z>CjQ\333\354?\2046\216K\337\307\354?\224\177\234\036\373\264\354?L\277iZ\235\242\354?_\214h\t\277\220\354?(\'\365\272Y\177\354?\325\276\206vgn\354?\361\264`\260\342]\354?\256I\217?\306M\354?\373M\003U\r>\354?\264=\247s\263.\354?b\302Mi\264\037\354?\366L^H\f\021\354?A_(b\267\002\354?gg\311A\262\364\353?\372\324\223\247\371\346\353?Ug\350\204\212\331\353?\027\262t\370a\314\353?/\206\313J}\277\353?U`M\353\331\262\353?\016;Xmu\246\353?\r3\267\205M\232\353?\246WK\b`\216\353?\022\314\347\345\252\202\353?\365\r\\*,w\353?l\316\247\372\341k\353?\245OT\223\312`\353?-\255\357F\344U\353?M\332\246|-K\353?\352z\373\256\244@\353?@\t\223jH6\353?\235\377\034M\027,\353?\312\371M\004\020\"\353?Y\367\355L1\030\353?r\027\370\361y\016\353?\273P\312\313\350\004\353?P\315c\277|\373\352?"
print(np.frombuffer(tensor_content, dtype=np.float64, count=60))

yields

[0.99270051 0.98595279 0.97968232 0.97382851 0.96834149 0.96317974
 0.95830831 0.95369753 0.949322   0.94515981 0.94119194 0.93740176
 0.93377466 0.93029776 0.9269596  0.92374998 0.92065977 0.91768075
 0.91480553 0.91202741 0.90934032 0.90673871 0.90421756 0.90177222
 0.89939847 0.8970924  0.89485042 0.89266922 0.89054572 0.88847707
 0.88646063 0.88449395 0.88257472 0.8807008  0.87887021 0.87708105
 0.87533158 0.87362016 0.87194522 0.87030531 0.86869906 0.86712517
 0.86558243 0.86406967 0.86258579 0.86112978 0.85970063 0.85829743
 0.85691928 0.85556535 0.85423483 0.85292696 0.85164103 0.85037633
 0.8491322  0.84790803 0.8467032  0.84551713 0.84434929 0.84319913]

Attempt to re-implement in Scala

byteString
  .toByteArray
  .grouped(groupSize)
  .map(ByteBuffer.wrap)
  .map(x => x.getLong().doubleValue())
//      .map((x: ByteBuffer) => x.getDouble)
  .toList

yields (1x60):

List(6.9890257572757069E17, -4.9254936884466852E18, 1.93766246328270413E18, 3.6263701026148147E18, -4.106473625017258E18, 1.83409848222711373E18, -2.5766556904182948E18, 1.07219606320709197E18, 8.838890971974398E18, -7.8929240631207987E17, 8.8900687228581274E18, 1.00084151737653197E18, -8.9689646167491912E18, -3.2706372030245811E18, -8.4730720576501228E18, 6.5109122717671055E18, -3.6044168036929171E17, 1.20769748039840102E18, -5.4600056618766213E18, -6.5354707976662589E18, 3.8434160952619781E18, 1.79200836232740403E18, 2.13869043690090854E18, 2.7967736194575191E18, -1.98617780470317542E18, 4.5185105169866378E18, 8.4570372703895501E18, -7.4631584498411817E18, 1.9689528917245248E18, 8.4241787499766912E17, 8.6082672574449531E18, 7.8610963577128172E18, -1.20422520877239987E18, -3.490837519165887E18, -4.2717110585424E18, -4.5617572118779704E18, -1.31507519536405376E18, 6.9527776938864753E18, 6.9838571554275277E18, -3.3562142372552422E18, -6.562930844162135E18, 3.498641661082857E18, 8.5992715656753265E18, -8.6693056958156467E17, 6.1770282981225871E18, -8.6466382289671342E18, 6.8231551058079201E18, -5.7794481962618112E18, 9.1087285368266373E18, -2.7215157173118618E18, -4.7843050888908486E17, -9.2759565910370406E17, -4.5995529861171169E18, 7.6403115199991593E18, -1.74754230110192128E18, -6.9056761263699446E18, -5.3044743132591405E17, -3.3741956467671747E18, -4.7610056397553715E18, 5.9983033819763784E18)

which clearly shows values are not similar to the numpy implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment