【问题标题】:How can I concatenate tensors and save it to a text file?如何连接张量并将其保存到文本文件中?
【发布时间】:2021-11-18 02:09:10
【问题描述】:

假设,我有四个不同数据类型的列表。我也有一个二维矩阵。我想按列合并它们。

即我有以下列表/矩阵:

[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]]

我想要一个文本文件中的以下输出:

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

我尝试了以下方法:

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)

输出

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

我怎样才能做到这一点?

【问题讨论】:

标签: python numpy tensorflow


【解决方案1】:

Pandas 可以连接这些列表和矩阵:

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')

【讨论】:

  • 你没有使用 TF。
【解决方案2】:
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()

【讨论】:

  • 你没有使用 TF。
  • 我对 TF 不熟悉,但我认为脚本可以在不依赖其他模块的情况下完成这项工作。
猜你喜欢
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2016-10-26
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多