'Exporting numpy array as csv imported to Tensorflow.js has no shape
I need to export numpy arrays from Python to Javascript for use in Tensorflow.js. I had a look here and tried to do something similar but my imported tensor has no shape, it's not loaded properly.
Export from Python using Pandas dataframe looked like this (simplified):
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
A_df = pd.DataFrame(A)
A_df.to_csv('tmp.csv')
The tmp.csv looks like this:
,0,1,2
0,1,2,3
1,4,5,6
2,7,8,9
3,10,11,12
In Javascript it's loaded from url like this, when I test it on my local server:
const url = 'https://127.0.0.1/[localurlpath...]/tmp.csv'
const cols = ["", "0", "1", "2"]
let csvConfig = {
hasHeader: true,
columnNames: cols
}
const A = tf.data.csv(url, csvConfig)
console.log(A.shape)
Console log says: "undefined"
I have tried:
delimiter: '\n',
delimWhitespace: true
But to no avail. I also tried other column names to avoid the empty position. I get no errors in the Javascript console.
Solution 1:[1]
Since the numpy arrays in question aren't that big, aprox. 1000x50 at most, I ended upp using this method instead. CSV seemed overly complicated in the situation. Simplified example:
Export from Python:
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
with open('mtrx.json', 'w') as outfile:
outfile.write(json.dumps(np.array(A).tolist()))
Import in Javascript:
const url = 'http://127.0.0.1/[...local filepath...]/mtrx.json'
const res = await fetch(url)
const mtrx = await JSON.parse(await res.text())
It turns out the imported mtrx object can be used in the tf.matMulcalculation I need to perform without any further conversion. I thought it needed to be converted to a tf.tensor. It was simpler than I thought. I still don't understand what's wrong with my first method but I'll save that for a rainy day.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oortone |
