8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

尽管设置正确,但仍无法在 GPU 模式下运行 TensorFlow

Sathish Manohar 2月前

94 0

问题:我一直尝试在 GPU 模式下运行 TensorFlow,但没有成功。当我运行以下代码来检查可用设备时:from tensorflow.python.client import device_libdef

问题:我一直尝试在 GPU 模式下运行 TensorFlow,但没有成功。当我运行以下代码来检查可用设备时:

from tensorflow.python.client import device_lib

def get_available_devices():
    local_device_protos = device_lib.list_local_devices()
    return [x.name for x in local_device_protos]

print(get_available_devices())

输出为:

['/device:CPU:0']

预期输出应该是:

['/device:CPU:0', '/device:GPU:0']

当我跑步时:

import tensorflow as tf

print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))

输出为:

Num GPUs Available:  0

以下是我的规格:

  • CUDA 版本:12.2
  • cuDNN 版本:8.9.2
  • GPU型号:Nvidia GeForce MX350
  • 操作系统:Windows 11
  • TensorFlow 版本:2.16.0

我已采取的步骤:

  1. 已安装 CUDA 12.2
  2. 下载并设置 cuDNN 8.9.2
  3. 在环境变量中设置CUDA路径
  4. 重启电脑

这里可能存在什么问题?我该如何解决它以确保 TensorFlow 识别并使用 GPU?

帖子版权声明 1、本帖标题:尽管设置正确,但仍无法在 GPU 模式下运行 TensorFlow
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Sathish Manohar在本站《tensorflow》版块原创发布, 转载请注明出处!
最新回复 (0)
  • nvcc:NVIDIA (R) Cuda 编译器驱动程序版权所有 (c) 2005-2023 NVIDIA Corporation2023 Cuda 编译工具,版本 12.2,V12.2.91 构建 cuda_12.2.r12.2/compiler.32965470_0

  • 我还检查了 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\include 中的 cudnn_version.h

  • 我正在学习 Tensorflow TF 教程 (https://www.tensorflow.org/text/tutorials/transformer),但使用的是我自己的数据。我的数据与文本无关,但它是带有开头的标记序列

    我正在学习 Tensorflow TF 教程( https://www.tensorflow.org/text/tutorials/transformer ),但使用的是我自己的数据。我的数据与文本无关,但它是带有起始标记和终止标记的标记序列。所有标记从 0 到 30(起始标记是 31,终止标记是 32)。序列的长度为 64(加上起始和终止标记,总共 66)。序列如下所示:

    tf.Tensor(
    [31 10 10 10 10 18 10 19 27 22  5 19 10 10 10 10 10 19 10 19 10  1  1 20
     22 15 12 26 14 22 17  3 10 14 22  9 25 25 20  7 19 28  4  7 15 14 13 25
     21 15 15 17 14 18 14 14 14 27 14 19 25 19  5  3 17 32], shape=(66,), dtype=int32)
    

    我的代码与教程中的代码非常相似,只有很小的变化:

    import pickle, os
     
    import numpy as np
    import tensorflow as tf
    from tensorflow import keras
    from keras import layers
     
    from src.utils.callbacks import VQTransCallback
    from src.models.layers import GlobalSelfAttention, CrossAttention, CausalSelfAttention
    from src.models.layers import TransformerFeedForward as FeedForward
     
     
     
    class PositionalEmbedding(tf.keras.layers.Layer):
        def __init__(self, vocab_size, d_model):
            super().__init__()
            self.d_model = d_model
            self.embedding = layers.Embedding(vocab_size, d_model, mask_zero=True) 
            self.pos_encoding = self.positional_encoding(length=66, depth=d_model)
     
        def compute_mask(self, *args, **kwargs):
            return self.embedding.compute_mask(*args, **kwargs)
     
        def positional_encoding(self, length, depth):
            depth = depth/2
            positions = np.arange(length)[:, np.newaxis]     # (seq, 1)
            depths = np.arange(depth)[np.newaxis, :]/depth   # (1, depth)
            angle_rates = 1 / (10000**depths)         # (1, depth)
            angle_rads = positions * angle_rates      # (pos, depth)
            pos_encoding = np.concatenate(
                [np.sin(angle_rads), np.cos(angle_rads)],
                axis=-1) 
            return tf.cast(pos_encoding, dtype=tf.float32)
     
        def call(self, x):
            length = tf.shape(x)[1]
            x = self.embedding(x)
            # This factor sets the relative scale of the embedding and positonal_encoding.
            x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
            x = x + self.pos_encoding[tf.newaxis, :length, :]
            return x
     
     
    class EncoderLayer(tf.keras.layers.Layer):
      def __init__(self,*, d_model, num_heads, dff, dropout_rate=0.1):
        super().__init__()
     
        self.self_attention = GlobalSelfAttention(
            num_heads=num_heads,
            key_dim=d_model,
            dropout=dropout_rate)
     
        self.ffn = FeedForward(d_model, dff)
        self.supports_masking = True
     
      def call(self, x):
        x = self.self_attention(x)
        x = self.ffn(x)
        return x
      
     
    class Encoder(tf.keras.layers.Layer):
      def __init__(self, *, num_layers, d_model, num_heads,
                   dff, vocab_size, dropout_rate=0.1):
        super().__init__()
     
        self.d_model = d_model
        self.num_layers = num_layers
        self.pos_embedding = PositionalEmbedding(vocab_size=vocab_size, d_model=d_model)
     
        self.enc_layers = [
            EncoderLayer(d_model=d_model,
                         num_heads=num_heads,
                         dff=dff,
                         dropout_rate=dropout_rate)
            for _ in range(num_layers)]
        self.dropout = tf.keras.layers.Dropout(dropout_rate)
        self.supports_masking = True
     
      def call(self, x):
        x = self.pos_embedding(x)  # Shape `(batch_size, d_model)`.
        x = self.dropout(x)
        for i in range(self.num_layers):
          x = self.enc_layers[i](x)
        return x  # Shape `(batch_size, seq_len, d_model)`.
     
     
    class DecoderLayer(tf.keras.layers.Layer):
      def __init__(self, *, d_model, num_heads, dff, dropout_rate=0.1):
        super(DecoderLayer, self).__init__()
     
        self.causal_self_attention = CausalSelfAttention(
            num_heads=num_heads,
            key_dim=d_model,
            dropout=dropout_rate)
        self.cross_attention = CrossAttention(
            num_heads=num_heads,
            key_dim=d_model,
            dropout=dropout_rate)
        self.ffn = FeedForward(d_model, dff)
        self.supports_masking = True
     
      def call(self, x, context):
        x = self.causal_self_attention(x=x)
        x = self.cross_attention(x=x, context=context)
        x = self.ffn(x)  # Shape `(batch_size, seq_len, d_model)`.
        return x
      
     
    class Decoder(tf.keras.layers.Layer):
      def __init__(self, *, num_layers, d_model, num_heads, dff, vocab_size,
                   dropout_rate=0.1):
        super(Decoder, self).__init__()
     
        self.d_model = d_model
        self.num_layers = num_layers
        self.pos_embedding = PositionalEmbedding(vocab_size=vocab_size, d_model=d_model)
        self.dropout = tf.keras.layers.Dropout(dropout_rate)
        self.dec_layers = [
            DecoderLayer(d_model=d_model, num_heads=num_heads,
                         dff=dff, dropout_rate=dropout_rate)
            for _ in range(num_layers)]
        self.supports_masking = True
     
      def call(self, x, context):
        x = self.pos_embedding(x)  # (batch_size, target_seq_len, d_model)
        x = self.dropout(x)
        for i in range(self.num_layers):
            x  = self.dec_layers[i](x, context)
        return x
     
     
    class VQVAE2Transformer(tf.keras.Model):
        def __init__(self, *, enc_num_layers, dec_num_layers, d_model, num_heads, dff,
                     vocab_size, codebook_length, dropout_rate=0.1):
            super().__init__()
            self.encoder = Encoder(num_layers=enc_num_layers, d_model=d_model,
                                    num_heads=num_heads, dff=dff, vocab_size=vocab_size,
                                    dropout_rate=dropout_rate)
            self.decoder = Decoder(num_layers=dec_num_layers, d_model=d_model,
                                    num_heads=num_heads, dff=dff, vocab_size=vocab_size,
                                    dropout_rate=dropout_rate)
            
            self.enc_num_layers = enc_num_layers
            self.dec_num_layers = dec_num_layers
            self.d_model = d_model
            self.num_heads = num_heads
            self.dff = dff
            self.vocab_size = vocab_size + 3 + 1 # +3 start, end and mask
            self.codebook_length = codebook_length + 2 # +2 start and end tokens
     
            self.start_token = vocab_size+1
            self.end_token = vocab_size+2
     
            self.final_layer = tf.keras.layers.Dense(self.vocab_size)
            self.supports_masking = True
     
     
        def call(self, inputs):
            enc_in, dec_in = inputs[0], inputs[1]
            
            context = self.encoder(enc_in)  # (batch_size, context_len, d_model)
            x = self.decoder(dec_in, context)  # (batch_size, target_len, d_model)
            # Final linear layer output.
            logits = self.final_layer(x)  # (batch_size, target_len, latent_size)
     
            try:
                # Drop the keras mask, so it doesn't scale the losses/metrics.
                # b/250038731
                del logits._keras_mask
            except AttributeError:
                pass
     
            # Return the final output and the attention weights.
            return logits
     
     
        def accuracy(self, label, pred):
            pred = tf.argmax(pred, axis=2)
            label = tf.cast(label, pred.dtype)
            match = label == pred
            match = tf.cast(match, dtype=tf.float32)
            return tf.reduce_sum(match) / self.codebook_length
     
     
        def compile_model(self):
            optimizer = keras.optimizers.AdamW()
            scce = tf.keras.losses.SparseCategoricalCrossentropy(
                from_logits=True)
     
            self.compile(optimizer=optimizer,
                         loss=scce,
                         metrics=["accuracy"])
     
     
        def save_build(self, folder):
            """Saves the config before the training starts. The model itself will be saved
            later on using keras checkpoints.
     
            Args:
                folder: Where to save the config parameters
            """
            if not os.path.exists(folder):
                os.makedirs(folder)
                os.makedirs(os.path.join(folder, 'weights'))
     
            with open(os.path.join(folder, 'params.pkl'), 'wb') as f:
                pickle.dump([
                    self.enc_num_layers,
                    self.dec_num_layers,
                    self.d_model,
                    self.num_heads,
                    self.dff,
                    self.vocab_size,
                ], f)
     
     
        def train_step(self, batch):
            """Processes one batch inside model.fit()."""
     
            enc_in, dec_in = batch[0][:,:66], batch[0][:,66:]
            dec_input = dec_in[:, :-1]
            dec_target = dec_in[:, 1:]
            with tf.GradientTape() as tape:
                preds = self([enc_in, dec_input])
                loss = self.compute_loss(None, dec_target, preds)
            trainable_vars = self.trainable_variables
            gradients = tape.gradient(loss, trainable_vars)
            self.optimizer.apply_gradients(zip(gradients, trainable_vars))
     
            # Update the metrics
            self.compiled_metrics.update_state(dec_target, preds)
            return {m.name: m.result() for m in self.metrics}
     
     
        def test_step(self, batch):
            enc_in, dec_in = batch[0][:,:66], batch[0][:,66:]
            dec_input = dec_in[:, :-1]
            dec_target = dec_in[:, 1:]
            preds = self([enc_in, dec_input])
            self.compiled_metrics.update_state(dec_target, preds)
            return {m.name: m.result() for m in self.metrics}
     
     
        def train(
            self, train_dataset, valid_dataset, epochs, run_folder, initial_epoch=0, 
            print_every_n_epochs=5
        ):
            
            test_batch = next(valid_dataset.dataset_iter)
            display_cb = VQTransCallback(test_batch, run_folder)
     
     
            checkpoint_filepath = os.path.join(
                    run_folder, "weights/{epoch:03d}-{loss:.5f}-{val_loss:.5f}.weights.h5")
            checkpoint1 = keras.callbacks.ModelCheckpoint(
                checkpoint_filepath, save_weights_only=True, save_best_only=True)
            checkpoint2 = keras.callbacks.ModelCheckpoint(
                os.path.join(run_folder, 'weights/last.weights.h5'),
                save_weights_only=True, save_best_only=True)
     
            callbacks_list = [checkpoint1, checkpoint2, display_cb]
     
            self.fit(
                train_dataset.dataset, validation_data=valid_dataset.dataset,
                epochs=epochs, initial_epoch=initial_epoch, callbacks=callbacks_list,
                #steps_per_epoch=1000, validation_steps=1000
                )
     
        
        def generate(self, enc_in, dec_in, startid=0):
            """Performs inference over one batch of inputs."""
            bs = tf.shape(enc_in)[0]
     
            # scce = tf.keras.losses.SparseCategoricalCrossentropy(
            #     from_logits=True)
            # logits = self([enc_in, dec_in[:,:-1]])
            # print("SCCE LOSS")
            # print(scce(dec_in[:,1:],logits))
     
            if startid == 0:
                dec_in = tf.ones((bs, 1), dtype=tf.int32) * self.start_token
            else:
                dec_in = tf.cast(dec_in[:,:startid], dtype=tf.int32)
     
            for _ in range(self.codebook_length - startid):
                logits = self([enc_in, dec_in])
                logits = tf.argmax(logits, axis=-1, output_type=tf.int32)
                last_logit = tf.expand_dims(logits[:, -1], axis=-1)
                dec_in = tf.concat([dec_in, last_logit], axis=-1)
     
            return dec_in
    

    我使用教程中的稀疏交叉熵。

    我遇到了一个问题,训练过程中显示的损失变为负值。例如现在显示为 -2.0593。

    我正在使用稀疏交叉熵。当我使用回调监控某些测试批次的损失时,返回的值永远不会为负数,但通常是 1.5 到 2 之间的某个值。

    如您所见,Transformer 中的最后一层是没有激活函数的密集层(如教程中所示),因此在损失中我设置了“from_logit=True”。我尝试在最后一层使用 softmax 激活,然后设置“from_logit=False”,但这似乎无法训练,并且它卡在损失 0.274 左右,并且永远不会移动。

    我不知道为什么损失会变成负数,因为当我测试损失函数时它似乎总是输出正数。

    总体来说训练的效果也不太好。

  • 谢谢!我会尝试使用 2.11 以下的 TensorFlow 版本,看看是否有效。

  • 我有一个对象和一个表示 1 的变量。它可以是更多,例如:1、2、3、4、5.....等等。我想要的是将以下值与对象中的第一个值进行比较,然后...

    我有一个对象和一个表示为值的变量 1 。它可以是更多,例如: 1,2,3,4,5.....etc.

    我想要的是将后面的值与对象中的第一个值进行比较,如果减去第一个值的结果大于或等于 1 ,则将这些值添加到新对象中。例如:第一个值是 10 。下一个值是 10.5 ,它是中最大的 10 。我们 10 从中 10.5 。结果是 0.5 。这个结果小于 1 ,所以我们忽略它。

    我们移动到下一个值,即 11 ,由于 11 大于 10.5 ,我们 10 从 中 11 ,而不是 10.5 从 中 11 。结果是 1 ,我们将其添加到新对象中,结果如下: {pr:10, pr:11}.

    我们移动到下一个值,即 11.3 ,由于 11.3 大于 11 ,我们 10 从 中 11.3 。结果是 1.3 ,现在我们将其添加到新对象中,结果如下: {pr:10, pr:11, pr:11.3} .

    我们移动到下一个值,即 12 ,它比前一个值 大。 11.3 从 中 10 减去 12 。结果是 2 ,现在我们将它添加到新对象中,结果如下: {pr:10, pr:11, pr:11.3, pr:12 } .

    我们移至下一个值,即 11.5 ,并且我们注意到该值小于前一个值 。 12 因此,我们不 11.5 从 中 10 ,而是从前一个值 中减去 , 12 结果是 0.5 ,因此我们不将其添加到新对象中。

    我们移动到下一个值,即 12 ,并且由于前一个值未添加到新对象中,我们将它与之前的值进行比较 11.5 ,即 12 ,并且由于值相等,我们不会向新对象添加任何内容。因此新对象保持原样。 {pr:10, pr:11, pr:11.3, pr:12} .

    我们移至下一个值,即 11 ,由于 11 小于 12 ,我们 11 从 中 12 ,结果为 1 ,因此我们将 添加 11 到新对象中。结果是: {pr:10, pr:11, pr:11.3, pr:12, pr:11} .

    我花了很多时间试图得出结论,但我无法得出结论。任何能帮助我的人,我都表示感谢,并感谢大家。

    let lists = [
      { id: "1", price: 10,  },
      { id: "2", price: 10.5 },
      { id: "3", price: 11,  },
      { id: "4", price: 11.3,},
      { id: "5", price: 12,  },
      { id: "6", price: 11.5 },
      { id: "7", price: 12,  },
      { id: "8", price: 11,  },
    ]
    
    let finalList = [
      { id: "1", price: 10,  },
      { id: "3", price: 11,  },
      { id: "4", price: 11.3,},
      { id: "5", price: 12,  },
      { id: "8", price: 11,  },
    ]
    
  • Jimi 2月前 0 只看Ta
    引用 7

    就好像每个步骤中的步骤不应小于 1。我仅使用减法来过滤值,有些值是我需要的,有些则不是。

  • 如果 11.3 大于 11,只要当前值大于前一个值,我们就将当前值与第一个值进行比较,但是当顺序发生逆转时,就像值 (id:6, 11.5) 与 (id:5, 12) 中发生的情况一样。当前值为 11.5,小于前一个值 12。情况发生了变化。当前值不再大于前一个值。因此,在下一个值中,即

  • @Akshay 是的,实际上代码不能按照我想要的方式工作,但是提供的答案给了我一些想法,对此我深表感激。

  • pat 2月前 0 只看Ta
    引用 10

    希望这可以帮助。

    const lists = [
      { id: "1", price: 10 },
      { id: "2", price: 10.5 },
      { id: "3", price: 11 },
      { id: "4", price: 11.3 },
      { id: "5", price: 12 },
      { id: "6", price: 11.5 },
      { id: "7", price: 12 },
      { id: "8", price: 11 },
    ];
    
    let finalList = [lists[0]]; // Start with the first item from lists
    
    const firstPrice = lists[0].price;
    for (let i = 1; i < lists.length; i++) {
      let diff; 
    
      const currentPrice = lists[i].price;
      const lastValueAdded = finalList[finalList.length-1].price;
    
      if(currentPrice > lastValueAdded){
          // Calculate the difference between the current price and the first price
          diff = Math.abs(lists[i].price - firstPrice);
        } else {
          // Calculate the difference between the current price and the last price added
          diff = Math.abs(lists[i].price - lists[i - 1].price);
      }
      // If the difference is greater than or equal to 1, add the current item to finalList
      if (diff >= 1) {
        finalList.push(lists[i]);
      }
    }
    
    console.log(finalList);
  • 您可以将 map 函数与用作累加器的数组一起使用,就像我们在 reduce .=>

    let c = [];
    let accu = []; //accumulator
    let result = []; //finalList
    
    lists.map((current) => {
    
      if (c.length == 0) {
        c.push(current);
        result.push(current);
        accu.push(current);
        return;
      }
    
      if (current.price < accu[accu.length - 1].price) {
        c.push(accu[accu.length - 1]);
      }
    
      let subS = Math.abs(current.price - c[c.length - 1].price);
    
      if (subS >= 1) {
        accu.push(current);
        result.push(current)
      }
    
    })
    
    console.log('finalList:', result)
    <script>
      let lists = [
      { id: "1", price: 10,  },
      { id: "2", price: 10.5 },
      { id: "3", price: 11,  },
      { id: "4", price: 11.3,},
      { id: "5", price: 12,  },
      { id: "6", price: 11.5 },
      { id: "7", price: 12,  },
      { id: "8", price: 11,  },
    ]
    </script>
  • 我正在尝试使用 yolov8n-pose.pt 预训练模型训练 YOLOv8 模型,并在 config.yaml 中采用以下配置:# Datapath:C:\Users\Denis\OneDrive\Documents\Project\Workout Assist...

    我正在尝试使用 yolov8n-pose.pt 预训练模型训练 YOLOv8 模型,其 config.yaml 中的配置如下:

    # Data
    path: C:\Users\Denis\OneDrive\Documents\Project\Workout Assistant\data
    train: images/train # train images (relative to 'path')
    val: images/val # val images (relative to 'path')
    
    # Keypoints
    kpt_shape: [8, 0] # [number of keypoints, number of dim]
    flip_idx: [0, 1, 2, 3, 4, 5, 6, 7] # keypoints to flip
    
    # Classes
    names:
        0: starting_pos
        1: lifting
    

    这是我训练模型的代码

    from ultralytics import YOLO
    
    model = YOLO('yolov8n-pose.pt')  # load a pretrained model (recommended for training)
    
    model.train(data='config.yaml', epochs=1, imgsz=640)
    

    我收到以下错误

    RuntimeError: Given groups=1, expected weight to be at least 1 at dimension 0, but got weight of size [0, 16, 1, 1] instead
    

    我尝试过的步骤:

    • 验证预训练的模型文件以确保其未损坏。
    • 仔细检查 config.yaml 文件是否存在任何格式问题。
    • 确保数据集路径和类名指定正确。

    kpt_shape 有关 flip_idx ,但我不确定如何解决它。

    如能得到有关如何修复此错误的任何帮助或指点,我们将不胜感激。

返回
作者最近主题: