Присвойте значение внутри tf.tensor

У меня есть объект predict_wide_window. В нем есть переменная example.

predict_wide_window.example value are:<tf.Tensor: shape=(1, 12, 4), dtype=float32, numpy=

     array([[[ 1.    , 13.521 ,  6.257 ,  8.6   ],
             [ 2.    , 12.692 ,  8.0317,  8.8   ],
             [ 3.    , 12.692 ,  9.0498,  8.4211],
             [ 4.    , 13.198 ,  9.7643,  8.85  ],
             [ 5.    , 13.198 ,  9.7996,  7.25  ],
             [ 6.    , 13.198 , 10.1545,  8.    ],
             [ 7.    , 16.136 , 10.1307,  8.    ],
             [ 8.    , 16.136 , 10.2481,  8.    ],
             [ 9.    , 16.136 ,  9.7015,  7.7   ],
             [10.    , 13.521 ,  9.2827,  7.95  ],
             [11.    , 13.521 ,  9.5588,  7.7   ],
             [12.    , 13.521 , 10.4541, 10.    ]]], dtype=float32)>,
     <tf.Tensor: shape=(1, 12, 1), dtype=float32, numpy=
     array([[[ 8.8   ],
             [ 8.4211],
             [ 8.85  ],
             [ 7.25  ],
             [ 8.    ],
             [ 8.    ],
             [ 8.    ],
             [ 7.7   ],
             [ 7.95  ],
             [ 7.7   ],
             [10.    ],
             [ 7.85  ]]], dtype=float32)>)

Цель: я хочу изменить значение в первой строке, т. е. predict_wide_window.example[0][0][0] с

<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 1.   , 13.521,  6.257,  8.6  ], dtype=float32)>

to [ 1., 15, 10, 5 ] .

Итак, finalpredict_wide_window.example[0][0][0] должно быть

<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 1.   , 15,  10,  5], dtype=float32)>

Я делаю

predict_wide_window.example[0][0][0]=np.array([ 1., 15,  10,  5  ])

ОШИБКА: но я получаю сообщение об ошибке

TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

Как я могу с этим справиться. Спасибо

См. также:  как вызвать событие onmouseover для элемента в javascript?
Понравилась статья? Поделиться с друзьями:
IT Шеф
Комментарии: 1
  1. user1517108

    Да, поскольку нетерпеливые тензоры являются неизменяемыми тензорами, что означает, что значение внутри не может быть изменено, вы должны использовать tf.Variable, если вы хотите создать тензор, который можно изменять как таковой.

    import tensorflow as tf
    import numpy as np
    
    pred_window = tf.reshape(tf.range(1 * 12 * 4, dtype=tf.float32), (1, 12, 4))
    pred_window_var = tf.Variable(pred_window)
    '''
    <tf.Tensor: shape=(1, 12, 4), dtype=float32, numpy=
    array([[[ 0.,  1.,  2.,  3.],
            [ 4.,  5.,  6.,  7.],
            [ 8.,  9., 10., 11.],
            [12., 13., 14., 15.],
            [16., 17., 18., 19.],
            [20., 21., 22., 23.],
            [24., 25., 26., 27.],
            [28., 29., 30., 31.],
            [32., 33., 34., 35.],
            [36., 37., 38., 39.],
            [40., 41., 42., 43.],
            [44., 45., 46., 47.]]], dtype=float32)>'''
    
    # Note that you must use the assign function along with the exact dimensions you are trying to fill up in this case here [1, 1, 4]
    pred_window_var[:, 0:1, :].assign(tf.constant([[[1, 1, 1, 1]]], dtype=tf.float32))
    pred_window_var[:, 1:2, :].assign(np.array([[[1, 1, 1, 2]]], dtype=np.float32))
    '''
    <tf.Variable 'UnreadVariable' shape=(1, 12, 4) dtype=float32, numpy=
    array([[[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  2.],
            [ 8.,  9., 10., 11.],
            [12., 13., 14., 15.],
            [16., 17., 18., 19.],
            [20., 21., 22., 23.],
            [24., 25., 26., 27.],
            [28., 29., 30., 31.],
            [32., 33., 34., 35.],
            [36., 37., 38., 39.],
            [40., 41., 42., 43.],
            [44., 45., 46., 47.]]], dtype=float32)>'''
    
Добавить комментарий

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: