Home > Net >  How can I concatenate tensors and save it to a text file?
How can I concatenate tensors and save it to a text file?

Time:09-25

Suppose, I have four lists of different data types. I also have a 2d matrix. I want to merge them column-wise.

I.e. I have the following lists/matrices:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['abcd', 'bcde', 'cdef', 'defg', 'efgh', 'fghi', 'ghij', 'hijk', 'ijkl', 'jklm']

[[0.0, 0.1, 0.2, 0.3],
 [0.1, 0.2, 0.3, 0.4],
 [0.2, 0.3, 0.4, 0.5],
 [0.3, 0.4, 0.5, 0.6],
 [0.4, 0.5, 0.6, 0.7],
 [0.5, 0.6, 0.7, 0.8],
 [0.6, 0.7, 0.8, 0.9],
 [ 0.7, 0.8, 0.9, 1.0],
 [0.8, 0.9, 1.0, 1.1],
 [0.9, 1.0, 1.1, 1.2]]

I want the following output in a text file:

1   a   abcd    0.0     0.1     0.2     0.3
2   b   bcde    0.1     0.2     0.3     0.4
3   c   cdef    0.2     0.3     0.4     0.5
4   d   defg    0.3     0.4     0.5     0.6
5   e   efgh    0.4     0.5     0.6     0.7
6   f   fghi    0.5     0.6     0.7     0.8
7   g   ghij    0.6     0.7     0.8     0.9
8   h   hijk    0.7     0.8     0.9     1.0
9   i   ijkl    0.8     0.9     1.0     1.1
0   j   jklm    0.9     1.0     1.1     1.2

I tried the following:

import os

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

import tensorflow as tf
import numpy as np

integers_list = tf.convert_to_tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
char_list = tf.convert_to_tensor(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
sting_list = tf.convert_to_tensor(['abcd', 'bcde', 'cdef', 'defg', 'efgh', 'fghi', 'ghij', 'hijk', 'ijkl', 'jklm'])
float_matrix = tf.convert_to_tensor([[0.0, 0.1, 0.2, 0.3],
                [0.1, 0.2, 0.3, 0.4],
                [0.2, 0.3, 0.4, 0.5],
                [0.3, 0.4, 0.5, 0.6],
                [0.4, 0.5, 0.6, 0.7],
                [0.5, 0.6, 0.7, 0.8],
                [0.6, 0.7, 0.8, 0.9],
                [ 0.7, 0.8, 0.9, 1.0],
                [0.8, 0.9, 1.0, 1.1],
                [0.9, 1.0, 1.1, 1.2]])

final_tensor = tf.concat([integers_list, char_list, sting_list, float_matrix], 1)

print(final_tensor)

tf.io.write_file("filename1.txt", final_tensor, name=None)

Output

C:\ProgramData\Miniconda3\python.exe C:/Users/pc/source/repos/my_concat_test/merge_object_np.py
Traceback (most recent call last):
  File "C:\Users\pc\source\repos\my_concat_test\merge_object_np.py", line 28, in <module>
    final_tensor = tf.concat([integers_list, char_list, sting_list, float_matrix], 1)
  File "C:\ProgramData\Miniconda3\lib\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
    return target(*args, **kwargs)
  File "C:\ProgramData\Miniconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1768, in concat
    return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
  File "C:\ProgramData\Miniconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1212, in concat_v2
    _ops.raise_from_not_ok_status(e, name)
  File "C:\ProgramData\Miniconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 6897, in raise_from_not_ok_status
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute ConcatV2 as input #1(zero-based) was expected to be a int32 tensor but is a string tensor [Op:ConcatV2] name: concat

Process finished with exit code 1

How can I achieve this?

CodePudding user response:

Pandas can concatenate these lists and matrix:

l1=[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
l2=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
l3=['abcd', 'bcde', 'cdef', 'defg', 'efgh', 'fghi', 'ghij', 'hijk', 'ijkl', 'jklm']

matrix=[[0.0, 0.1, 0.2, 0.3],
 [0.1, 0.2, 0.3, 0.4],
 [0.2, 0.3, 0.4, 0.5],
 [0.3, 0.4, 0.5, 0.6],
 [0.4, 0.5, 0.6, 0.7],
 [0.5, 0.6, 0.7, 0.8],
 [0.6, 0.7, 0.8, 0.9],
 [ 0.7, 0.8, 0.9, 1.0],
 [0.8, 0.9, 1.0, 1.1],
 [0.9, 1.0, 1.1, 1.2]]

import pandas as pd
df=pd.concat([pd.DataFrame(l1),
        pd.DataFrame(l2),
        pd.DataFrame(l3),
       pd.DataFrame(matrix)],axis=1)

np.savetxt(r'np.txt', df.values, fmt='%s')

CodePudding user response:

l= [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
m = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
v = ['abcd', 'bcde', 'cdef', 'defg', 'efgh', 'fghi', 'ghij', 'hijk', 'ijkl', 'jklm']

arr = [[0.0, 0.1, 0.2, 0.3],
 [0.1, 0.2, 0.3, 0.4],
 [0.2, 0.3, 0.4, 0.5],
 [0.3, 0.4, 0.5, 0.6],
 [0.4, 0.5, 0.6, 0.7],
 [0.5, 0.6, 0.7, 0.8],
 [0.6, 0.7, 0.8, 0.9],
 [ 0.7, 0.8, 0.9, 1.0],
 [0.8, 0.9, 1.0, 1.1],
 [0.9, 1.0, 1.1, 1.2]]

outputs = []
for i in range(len(l)):
    g = ''
    for k in arr[i]:
        g  = str(k)   '     '
    outputs.append(str(l[i])   '   '  m[i]   '   '  v[i]   '    '   g  '\n')

# This is to save your outputs to a desired file txt :

k = open(r"filename1.txt", mode = 'w')
k.writelines(outputs)
k.close()

output in the txt file

  • Related