ValueError:Shape必须是等级2,但对于' MatMul'

时间:2018-05-03 19:44:40

标签: python tensorflow machine-learning

我有以下TensorFlow代码:

layer_1 = tf.add(tf.matmul(tf.cast(x, tf.float32), weights['h1']), biases['b1'])

但是引发了以下错误:

ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [?,5741,20000], [20000,128].

它说x的形状为(?,5741,20000)。我怎样才能将x的形状转换为(5741,20000)?

提前谢谢!

3 个答案:

答案 0 :(得分:2)

我建议使用张量点积而不是简单矩阵乘法来保持批量大小。答案比@mrry

更通用
import React from 'react';

const CharData = props => {
  return (
    <React.Fragment>

      {
        props.charData.map((char, ind) => {
          return (
            <div className='char-card-container__char-card-wrapper'>
              <div key={ind + Date.now()} className="char-card-container__char-card">

                <h2 className='mt-2'>{char.name}</h2>
                <p className='mt-2'><span className='char-card__char-info-label'>Gender:</span> {char.gender}</p>
                <p className='mt-2'><span className='char-card__char-info-label'>Birth Year:</span> {char.birth_year}</p>
                <p className='mt-2'><span className='char-card__char-info-label'>Eye Color:</span> {char.eye_color}</p>
                <p className='mt-2'><span className='char-card__char-info-label'>Hair Color:</span> {char.hair_color}</p>
                <p className='mt-2'><span className='char-card__char-info-label'>Height:</span> {char.height}</p>
                <p className='mt-2'><span className='char-card__char-info-label'>Weight:</span> {char.mass}</p>
                <p className='mt-2'><span className='char-card__char-info-label'>Skin Color:</span> {char.skin_color}</p>

                <div className='mt-2'>
                  <div>
                    <p className='char-card__char-info-label'>Films</p>
                  </div>
                  <div>
                    {
                      char.films.map(film => {
                        return (
                          <p>{film}</p>
                        )
                      })
                    }
                  </div>
                </div>

                <div className='mt-2'>
                  <div>
                    <p className='char-card__char-info-label'>Specie</p>
                  </div>
                  <div>
                    {
                      char.species.map(specie => {
                        return (
                          <p>{specie}</p>
                        )
                      })
                    }
                  </div>
                </div>

                <div className='mt-2'>
                  <div>
                    <p className='char-card__char-info-label'>StarShips</p>
                  </div>
                  <div>
                    {
                      char.starships.map(starship => {
                        return (
                          <p>{starship}</p>
                        )
                      })
                    }
                  </div>
                </div>

                <div className='mt-2'>
                  <div>
                    <p className='char-card__char-info-label'>Vehicles</p>
                  </div>
                  <div>
                    {
                      char.vehicles.map(vehicle => {
                        return (
                          <p>{vehicle}</p>
                        )
                      })
                    }
                  </div>
                </div>

              </div>
            </div>
          );
        })
      }

    </React.Fragment>
  );
}

export default CharData;

答案 1 :(得分:0)

看起来你正在尝试矩阵乘法&#39; x&#39;使用&#39;权重&#39;。对于一个示例,x的形状为[5741,20000],但是当您批量提供示例时,x将具有[?,5741,20000]的形状。同样,权重也应该具有[?,20000,128]的形状。但是,从错误中看,你的权重看起来仍然是[20000,128],这告诉我你的代码中有一些问题没有将权重变量转换为形状[?,20000,128]。当你能想到这一点时,错误就会消失。矩阵乘法的结果应该具有[?,5741,128]

的形状

答案 2 :(得分:-1)

假设x x的动态形状为(1, 5741, 20000),您可以使用tf.squeeze()将其形状转换为(5741, 20000),如下所示:

x = tf.squeeze(x, axis=[0])